他们是一种方式,我可以通过名称和子节点名称过滤json并构造一个新的输出JSON。因为这是一个很长的JSON我在这里有它
从上面这个JSON我想过滤并构造下面的JSON:
{
"name": "Age",
"code": "Age",
"children": [{
"name": "Female",
"code": "Age_Female",
"children": [{
"name": "Female   Under 5 years",
"code": "Q012027",
"count": 242
}, {
"name": "Female  5 to 13 years",
"code": "Q012028",
"count": 242
}, {
"name": "Female 14 to 17 years",
"code": "Q012030",
"count": 242
}, {
"name": "Female 18 to 21 years",
"code": "Q012031",
"count": 242
}, {
"name": "Single Year",
"code": "FEMALE_SINGLE_YEAR",
"children": [{
"name": "Female Single Year Female Under 1 year",
"code": "Q012A27",
"count": 242
}, {
"name": "Female Single Year Female 1 year",
"code": "Q012B27",
"count": 242
}, {
"name": "Female Single Year Female 2 years",
"code": "Q012C27",
"count": 242
}, {
"name": "Female Single Year Female 3 years",
"code": "Q012D27",
"count": 242
}, {
"name": "Female Single Year Female 4 years",
"code": "Q012E27",
"count": 242
}, {
"name": "Female Single Year Female 5 years",
"code": "Q012A28",
"count": 242
}, {
"name": "Female Single Year Female 6 years",
"code": "Q012B28",
"count": 242
}, {
"name": "Female Single Year Female 7 years",
"code": "Q012C28",
"count": 242
}, {
"name": "Female Single Year Female 8 years",
"code": "Q012D28",
"count": 242
}, {
"name": "Female Single Year Female 9 years",
"code": "Q012E28",
"count": 242
}, {
"name": "Female Single Year Female 10 years",
"code": "Q012F28",
"count": 242
}, {
"name": "Female Single Year Female 11 years",
"code": "Q012G28",
"count": 242
}, {
"name": "Female Single Year Female 12 years",
"code": "Q012H28",
"count": 242
}, {
"name": "Female Single Year Female 13 years",
"code": "Q012I28",
"count": 242
}, {
"name": "Female Single Year Female 14 years",
"code": "Q012A30",
"count": 242
}, {
"name": "Female Single Year Female 15 years",
"code": "Q012B30",
"count": 242
}, {
"name": "Female Single Year Female 16 years",
"code": "Q012C30",
"count": 242
}, {
"name": "Female Single Year Female 17 years",
"code": "Q012D30",
"count": 242
}, {
"name": "Female Single Year Female 18 years",
"code": "Q012A31",
"count": 242
}, {
"name": "Female Single Year Female 19 years",
"code": "Q012B31",
"count": 242
}, {
"name": "Female Single Year Female 20 years",
"code": "Q012C31",
"count": 242
}, {
"name": "Female Single Year Female 21 years",
"code": "Q012D31",
"count": 242
}]
}, {
"name": "Female 22 to 24 years",
"code": "Q012033",
"count": 242
}, {
"name": "Female 25 to 29 years",
"code": "Q012035",
"count": 242
}, {
"name": "Female 30 to 34 years",
"code": "Q012036",
"count": 242
}, {
"name": "Female 35 to 39 years",
"code": "Q012037",
"count": 242
}, {
"name": "Female 40 to 44 years",
"code": "Q012038",
"count": 242
}, {
"name": "Female 45 to 49 years",
"code": "Q012039",
"count": 242
}, {
"name": "Female 50 to 54 years",
"code": "Q012040",
"count": 242
}, {
"name": "Female 55 to 59 years",
"code": "Q012041",
"count": 242
}, {
"name": "Female 60 to 64 years",
"code": "Q012042",
"count": 242
}, {
"name": "Female 65 to 69 years",
"code": "Q012044",
"count": 242
}, {
"name": "Female 70 to 74 years",
"code": "Q012046",
"count": 242
}, {
"name": "Female 75 to 79 years",
"code": "Q012047",
"count": 242
}, {
"name": "Female 80 to 84 years",
"code": "Q012048",
"count": 242
}, {
"name": "Female 85 years plus",
"code": "Q012049",
"count": 242
}, {
"name": "Female 18 years plus",
"code": "Q0120F_18P",
"count": 242
}, {
"name": "Aggregate Age Female",
"code": "Q013B_3",
"count": 242
}, {
"name": "Female Average Age",
"code": "Q013B_3AV",
"count": 242
}, {
"name": "Female Median Age",
"code": "R013B_3MD",
"count": 242
}]
}, {
"name": "Male",
"code": "Age_Male",
"children": [{
"name": "Aggregate Age Male",
"code": "Q013B_2",
"count": 242
}, {
"name": "Male   Under 5 years",
"code": "Q012003",
"count": 242
}, {
"name": "Male  5 to 13 years",
"code": "Q012004",
}, {
"count": 242
"name": "Single Year",
"code": "MALE_SINGLE_YEAR",
"children": [{
"name": "Male Single Year Male Under 1 year",
"code": "Q012A03",
"count": 242
}, {
"name": "Male Single Year Male 1 year",
"code": "Q012B03",
"count": 242
}, {
"name": "Male Single Year Male 2 years",
"code": "Q012C03",
"count": 242
}, {
"name": "Male Single Year Male 3 years",
"code": "Q012D03",
"count": 242
}, {
"name": "Male Single Year Male 4 years",
"code": "Q012E03",
"count": 242
}, {
"name": "Male Single Year Male 5 years",
"code": "Q012A04",
"count": 242
}, {
"name": "Male Single Year Male 6 years",
"code": "Q012B04",
"count": 242
}, {
"name": "Male Single Year Male 7 years",
"code": "Q012C04",
"count": 242
}, {
"name": "Male Single Year Male 8 years",
"code": "Q012D04",
"count": 242
}, {
"name": "Male Single Year Male 9 years",
"code": "Q012E04",
"count": 242
}, {
"name": "Male Single Year Male 10 years",
"code": "Q012F04",
"count": 242
}, {
"name": "Male Single Year Male 11 years",
"code": "Q012G04",
"count": 242
}, {
"name": "Male Single Year Male 12 years",
"code": "Q012H04",
"count": 242
}, {
"name": "Male Single Year Male 13 years",
"code": "Q012I04",
"count": 242
}, {
"name": "Male Single Year Male 14 years",
"code": "Q012A06",
"count": 242
}, {
"name": "Male Single Year Male 15 years",
"code": "Q012B06",
"count": 242
}, {
"name": "Male Single Year Male 16 years",
"code": "Q012C06",
"count": 242
}, {
"name": "Male Single Year Male 17 years",
"code": "Q012D06",
"count": 242
}, {
"name": "Male Single Year Male 18 years",
"code": "Q012A07",
"count": 242
}, {
"name": "Male Single Year Male 19 years",
"code": "Q012B07",
"count": 242
}, {
"name": "Male Single Year Male 20 years",
"code": "Q012C07",
"count": 242
}, {
"name": "Male Single Year Male 21 years",
"code": "Q012D07",
"count": 242
}]
}, {
"name": "Male 14 to 17 years",
"code": "Q012006",
"count": 242
}, {
"name": "Male 18 to 21 years",
"code": "Q012007",
"count": 242
}, {
"name": "Male 18 years plus",
"code": "Q0120M_18P",
"count": 242
}, {
"name": "Male 22 to 24 years",
"code": "Q012009",
"count": 242
}, {
"name": "Male 25 to 29 years",
"code": "Q012011",
"count": 242
}, {
"name": "Male 30 to 34 years",
"code": "Q012012",
"count": 242
}, {
"name": "Male 35 to 39 years",
"code": "Q012013",
"count": 242
}, {
"name": "Male 40 to 44 years",
"code": "Q012014",
"count": 242
}, {
"name": "Male 45 to 49 years",
"code": "Q012015",
"count": 242
}, {
"name": "Male 50 to 54 years",
"code": "Q012016",
"count": 242
}, {
"name": "Male 55 to 59 years",
"code": "Q012017",
"count": 242
}, {
"name": "Male 60 to 64 years",
"code": "Q012018",
"count": 242
}, {
"name": "Male 65 to 69 years",
"code": "Q012020",
"count": 242
}, {
"name": "Male 70 to 74 years",
"code": "Q012022",
"count": 242
}, {
"name": "Male 75 to 79 years",
"code": "Q012023",
"count": 242
}, {
"name": "Male 80 to 84 years",
"code": "Q012024",
"count": 242
}, {
"name": "Male 85 years plus",
"code": "Q012025",
"count": 242
}, {
"name": "Male Average Age",
"code": "Q013B_2AV",
"count": 242
}, {
"name": "Male Median Age",
"code": "Q013B_2MD",
"count": 242
}]
}]
}]
}
答案 0 :(得分:4)
不确定
newJson = oldJson
.filter( obj => ["Raised","Old Fashioned"].indexOf(obj.name) !== -1 )
.map( obj => obj.topping )
这将为您提供一系列"顶部"来自raised
和old fashioned
个对象。
答案 1 :(得分:2)
您现在可以采取以下措施:
result = data.children
.filter(o => o.name === 'Age')
.map(o => Object.assign(
{},
o,
{ children: o.children.filter(p => ['Female', 'Male'].includes(p.name)) }
));