简体   繁体   English

将对象数组展平和 object 到对象数组

[英]Flattening and object with an array of objects in to an array of objects

I have a GraphQL endpoint that returns data (called locations) in the following format;我有一个 GraphQL 端点,它以以下格式返回数据(称为位置);

[
    {
        "name": "Location 1",
        "description": "-",
        "state": "Georgia",
        "address": "Kennesaw, GA 30144",
        "services": {
            "nodes": [
                {
                    "id": "cG9zdDo0OQ==",
                    "name": "Window Cleaning",
                    "color": "#00A7E3"
                }
            ]
        },
    },
    {
        "name": "Location 2",
        "description": "-",
        "state": "California",
        "address": "Los Angeles, 90016",
        "services": {
            "nodes": [
                {
                    "id": "cG9zdDo1Mg==",
                    "name": "Large Project Waterproofing",
                    "color": "#00668A"
                },
                {
                    "id": "cG9zdDo1MA==",
                    "name": "Surfaces, Stone & Metal Refinishing",
                    "color": "#333333"
                },
                {
                    "id": "cG9zdDo0OQ==",
                    "name": "Window Cleaning",
                    "color": "#00A7E3"
                }
            ]
        },
    },
]

What I would like to do is "flatten" it so that service becomes the array of objects and nodes is no longer there.我想做的是“扁平化”它,以便服务成为对象数组并且节点不再存在。 So the expected result would be;所以预期的结果是;

[
    {
        "name": "Location 1",
        "description": "-",
        "state": "Georgia",
        "address": "Kennesaw, GA 30144",
        "services": [
                {
                    "id": "cG9zdDo0OQ==",
                    "name": "Window Cleaning",
                    "color": "#00A7E3"
                }
        ]
    },
    {
        "name": "Location 2",
        "description": "-",
        "state": "California",
        "address": "Los Angeles, 90016",
        "services": [
                {
                    "id": "cG9zdDo1Mg==",
                    "name": "Large Project Waterproofing",
                    "color": "#00668A"
                },
                {
                    "id": "cG9zdDo1MA==",
                    "name": "Surfaces, Stone & Metal Refinishing",
                    "color": "#333333"
                },
                {
                    "id": "cG9zdDo0OQ==",
                    "name": "Window Cleaning",
                    "color": "#00A7E3"
                }
        ]
    },
]

I have tried looking at using the array.map method to do this with something like the following;我尝试过使用 array.map 方法来执行以下操作;

const locations = locations.map((location) =>
    location.services.nodes.map((service) => service)
);

Map the array to a new one where the services.nodes array is set into services Map 数组到一个新的数组,其中services.nodes数组设置为services

const newLocations = locations.map(({ services: { nodes }, ...location }) => ({
  ...location,
  services: nodes,
}));

 const locations = [{"name":"Location 1","description":"-","state":"Georgia","address":"Kennesaw, GA 30144","services":{"nodes":[{"id":"cG9zdDo0OQ==","name":"Window Cleaning","color":"#00A7E3"}]}},{"name":"Location 2","description":"-","state":"California","address":"Los Angeles, 90016","services":{"nodes":[{"id":"cG9zdDo1Mg==","name":"Large Project Waterproofing","color":"#00668A"},{"id":"cG9zdDo1MA==","name":"Surfaces, Stone & Metal Refinishing","color":"#333333"},{"id":"cG9zdDo0OQ==","name":"Window Cleaning","color":"#00A7E3"}]}}]; const newLocations = locations.map(({ services: { nodes }, ...location }) => ({...location, services: nodes, })); console.log(newLocations);
 .as-console-wrapper { max-height: 100%;important; }

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

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