简体   繁体   中英

Convert nested Array of Objects into Array of Arrays

I have to convert this array of objects into an array of arrays. I just want to get the value from "value" key and remove the value from "id" key.

And this is my response data:

[
    {
        "id": "f2e77e46-814b-4d73-83db-cb57c341d28f",
        "value": [
            {
                "id": "ba50429d-564a-404c-ba48-e70b9a3971d9",
                "value": 1
            },
            {
                "id": "d9b49f4a-f184-4dc5-9469-1ad52e23d2dc",
                "value": 1
            },
            {
                "id": "e1cf76aa-6516-4ed9-bdf7-c4d0d8f07102",
                "value": 1
            }
        ]
    },
    {
        "id": "baacce8d-192c-4353-bef9-9140c611f1d7",
        "value": [
            {
                "id": "b3bf9a68-4abf-43b2-9e3c-2cad0536a7f1",
                "value": 1
            },
            {
                "id": "7dbe6bad-1a81-45e1-b683-95a185b53f35",
                "value": 1
            },
            {
                "id": "c5282246-cc18-4ffa-bb57-459df6ae28b9",
                "value": 1
            }
        ]
    },
    {
        "id": "1e622287-915d-450b-8ed0-53b4a434030f",
        "value": [
            {
                "id": "2c69caf6-9277-4fcd-9d12-0d0823eb2805",
                "value": 1
            },
            {
                "id": "9a665798-1318-4662-9a17-abbc6ee77df7",
                "value": 1
            },
            {
                "id": "786157e5-45e3-4886-8307-f613ab8fcad0",
                "value": 1
            }
        ]
    }
]

And this is what i want it from my response data

[
[1,1,1],
[1,1,1],
[1,1,1]
]

I've tried using array map function but this is hard for me and always fail to convert it.

Use Array#map to extract the value property of the individual items and use a nested map to extract the value property of the value array:

 const data=[{id:"f2e77e46-814b-4d73-83db-cb57c341d28f",value:[{id:"ba50429d-564a-404c-ba48-e70b9a3971d9",value:1},{id:"d9b49f4a-f184-4dc5-9469-1ad52e23d2dc",value:1},{id:"e1cf76aa-6516-4ed9-bdf7-c4d0d8f07102",value:1}]},{id:"baacce8d-192c-4353-bef9-9140c611f1d7",value:[{id:"b3bf9a68-4abf-43b2-9e3c-2cad0536a7f1",value:1},{id:"7dbe6bad-1a81-45e1-b683-95a185b53f35",value:1},{id:"c5282246-cc18-4ffa-bb57-459df6ae28b9",value:1}]},{id:"1e622287-915d-450b-8ed0-53b4a434030f",value:[{id:"2c69caf6-9277-4fcd-9d12-0d0823eb2805",value:1},{id:"9a665798-1318-4662-9a17-abbc6ee77df7",value:1},{id:"786157e5-45e3-4886-8307-f613ab8fcad0",value:1}]}]; const result = data.map(e => e.value.map(f => f.value)); console.log(result);

You have an array of objects that contains a value array which also contains objects. So you need to map over the array of objects, and also map over the value array in those object to return the value of each value property.

 const arr=[{id:"f2e77e46-814b-4d73-83db-cb57c341d28f",value:[{id:"ba50429d-564a-404c-ba48-e70b9a3971d9",value:1},{id:"d9b49f4a-f184-4dc5-9469-1ad52e23d2dc",value:1},{id:"e1cf76aa-6516-4ed9-bdf7-c4d0d8f07102",value:1}]},{id:"baacce8d-192c-4353-bef9-9140c611f1d7",value:[{id:"b3bf9a68-4abf-43b2-9e3c-2cad0536a7f1",value:1},{id:"7dbe6bad-1a81-45e1-b683-95a185b53f35",value:1},{id:"c5282246-cc18-4ffa-bb57-459df6ae28b9",value:1}]},{id:"1e622287-915d-450b-8ed0-53b4a434030f",value:[{id:"2c69caf6-9277-4fcd-9d12-0d0823eb2805",value:1},{id:"9a665798-1318-4662-9a17-abbc6ee77df7",value:1},{id:"786157e5-45e3-4886-8307-f613ab8fcad0",value:1}]}]; const out = arr.map(obj => { return obj.value.map(arr => arr.value); }); console.log(out);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM