【发布时间】:2016-10-21 14:29:49
【问题描述】:
我有一个像这样的 4 级嵌套对象
{
"name": "“Mike”",
"id": 1000,
"img": "“#”",
"children": [
{
"name": "“Jack”",
"id": 1100,
"img": "#",
"married": true,
"children": [
{
"name": "”Julia“",
"id": 1110,
"img": "#"
}
]
},
{
"name": "”Lily“",
"id": 1200,
"img": "#",
"married": true,
"children": [
{
"name": "”Coco“",
"id": 1210,
"img": "#",
"married": true,
"children": {
"name": "”Olivia“",
"id": 1211,
"img": "#",
"married": false
}
}
]
}
]
}
我有一个 idToDisplay = ["1000","1100","1200","1210"] 数组
我想过滤掉 ID 并去掉不匹配 idToDisplay 的部分,这样我就可以得到一个看起来像这样的新对象
{
"name": "“Mike”",
"id": 1000,
"img": "“#”",
"children": [
{
"name": "“Jack”",
"id": 1100,
"img": "#",
"married": true,
]
},
{
"name": "”Lily“",
"id": 1200,
"img": "#",
"married": true,
"children": [
{
"name": "”Coco“",
"id": 1210,
"img": "#",
"married": true,
}
]
}
]
}
迭代的最佳方式是什么?非常感谢!
【问题讨论】:
标签: json loops object filter lodash