How to extract only the JSON values by removing the keys

-3

I have an array with some ids, the return object of my request to the WebService is as shown in code snippet number 1.

I would like to know if there is any way via jQuery, to just extract the values and leave my JSON as shown by the object of the second code snippet:

  • How are you?

       [{
                "id": "1"
            }, {
                "id": "2"
            }, {
                "id": "3"
            }, {
                "id": "4"
            }, {
                "id": "5"
            }]
    
  • How I wish you were:

    ["1","2","3","4","5"]
    
  • asked by anonymous 27.09.2018 / 15:15

    1 answer

    2

    You can use the Array.map method to extract only ids .

    Example:

    let dados = [
        { "id": "1" },
        { "id": "2" },
        { "id": "3" },
        { "id": "4" },
        { "id": "5" }
    ];
    
    let ids = dados.map(item => item.id);
    
    console.log(ids);

    If you can not use arrow functions , another example:

    var dados = [
        { "id": "1" },
        { "id": "2" },
        { "id": "3" },
        { "id": "4" },
        { "id": "5" }
    ];
    
    var ids = dados.map(function(item) {
        return item.id;
    });
    
    console.log(ids);
        
    27.09.2018 / 15:22