简体   繁体   English

将嵌套的对象数组转换为数组数组

[英]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.我只想从“value”键中获取值并从“id”键中删除值。

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:使用Array#map提取单个项的value属性,并使用嵌套map提取value数组的value属性:

 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.您有一个对象数组,其中包含一个包含对象的value数组。 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.所以,你需要map对象阵列上,map了在value数组中的对象返回各自的价值value属性。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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