【问题标题】:How do i take items from an array and nest them in the correct order?如何从数组中获取项目并以正确的顺序嵌套它们?
【发布时间】:2020-06-15 19:26:25
【问题描述】:

所以我无法解决这个问题。我有以下数组:

var array1 = [
USA,
Georgia,
Atlanta
]

var array2 = [
USA,
New York
]

这就是我想要的:

{
    "counties": [
        {
            "name": "USA",
            "id": "1",
            "children": [
                {
                    "name": "Georgia",
                    "id": "1.1",
                    "parentId": "1",
                    "children": [
                        {
                            "name": "Atlanta",
                            "id": "1.1.1",
                            "parentId": "1.1"
                        }
                    ]
                },
                {
                    "name": "New York",
                    "id": "1.2",
                    "parentId": "1"
                }
            ]
        }
    ]
}

诀窍是不要有任何重复,如果在同一级别上有任何重复,则应将它们合并为一个,并将两者的孩子都放在下面。就像例子中的美国。

【问题讨论】:

  • 您想在哪里使用 Vanilla JavaScript 或 NodeJs 的响应来实现此类对象?
  • 到目前为止你有什么尝试?
  • @PHPnoob 我尝试过的最接近的方法是遍历数组并将项目推送到“国家”,但我不知道如何将项目递归嵌套到子数组中。
  • @Soham Lawar - Kito 建议的 shape-json 模块看起来非常有趣。我会安装它并尝试一下,也许这就是解决方案。

标签: javascript arrays node.js json object


【解决方案1】:

我有这个transform 方法。在参数中传递任意数量的数组:

演示:

const array1 = [
    'USA',
    'Georgia',
    'Atlanta'
]

const array2 = [
    'USA',
    'New York'
]



const transform = (...arrays) => {
    const result = [];

    arrays.forEach(array => {
        let node = result;
        let parentID = ''

        array.forEach(item => {

            const current = node.find(c => c.name === item);

            if(current){
                node = current.children
            }else{
                const newNode = {
                    name: item,
                    children: [],
                    id: parentID === '' ? (node.length+1) + '' : parentID + '.'  + (node.length+1),
                    parentID
                };

                parentID = newNode.id;
                node.push(newNode);
                node = newNode.children
            }

        })
    })

    return {counties: result}
}

const result = transform(array1, array2);

console.log(result);

// {
//     "counties": [
//         {
//             "name": "USA",
//             "id": "1",
//             "parentID": "",
//             "children": [
//                 {
//                     "name": "Georgia",
//                     "id": "1.1",
//                     "parentID": "1",
//                     "children": [
//                         {
//                             "name": "Atlanta",
//                             "id": "1.1.1",
//                             "parentID": "1.1",
//                             "children": []
//                         }
//                     ]

//                 },
//                 {
//                     "name": "New York",
//                     "children": [],
//                     "id": "2",
//                     "parentID": ""
//                 }
//             ]

//         }
//     ]
// }

【讨论】:

  • 天才!非常非常感谢!
猜你喜欢
  • 2020-03-03
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 2013-12-12
  • 1970-01-01
相关资源
最近更新 更多