所以我有这个json。
{
"account_owner": "",
"account_type": "",
"nest_uid": "17_15_1536914882_yhHDzQsDSI",
"business_name": "",
"sync_block": false,
"validation": {
"isError": false,
"inputList": [],
"message": ""
},
"is_selected": true,
"contacts": [{
"con_title": "",
"con_fName": "",
"con_lName": "",
"con_job_title": "",
"is_selected": true,
"emails": [{
"email": "",
"type": "",
"primary": false,
"is_selected": true,
"nest_uid": "17_15_1536914882_yhHDzQsDSK",
"validation": {
"isError": false,
"inputList": [],
"message": "",
"is_selected": true
},
"checked": false
}],
"phones": [{
"phone": "",
"type": "",
"primary": false,
"is_selected": true,
"nest_uid": "17_15_1536914882_uHN38SxJ3s",
"validation": {
"isError": false,
"inputList": [],
"is_selected": true,
"message": ""
},
"checked": false
}],
"nest_uid": "17_15_1536914882_hwzB7dIn9v",
"checked": false
},
{
"con_title": "",
"con_fName": "",
"con_lName": "",
"con_job_title": "",
"is_selected": true,
"emails": [{
"email": "",
"type": "",
"primary": false,
"is_selected": true,
"nest_uid": "17_15_1536914882_yhHDzQsDSx",
"validation": {
"isError": false,
"is_selected": true,
"inputList": [],
"message": ""
},
"checked": false
}],
"phones": [{
"phone": "",
"type": "",
"primary": false,
"is_selected": true,
"nest_uid": "17_15_1536914882_uHN38SxJ3Y",
"validation": {
"isError": false,
"is_selected": true,
"inputList": [],
"message": ""
},
"checked": false
}],
"nest_uid": "17_15_1536914882_hwzB7dIn9x",
"checked": false
}
]
}
您会看到每个级别都有两个属性
nest_uid
is_selected
现在,我得到子元素的nest_uid
,因此我需要将所有is_selected
标记为false,除了所选元素及其父级。
例如
如果nest_id = '17_15_1536914882_yhHDzQsDSK'
输出
{
"account_owner": "",
"account_type": "",
"nest_uid": "17_15_1536914882_yhHDzQsDSI",
"business_name": "",
"sync_block": false,
"validation": {
"isError": false,
"inputList": [],
"message": ""
},
"is_selected": true,//here
"contacts": [{
"con_title": "",
"con_fName": "",
"con_lName": "",
"con_job_title": "",
"is_selected": true, //here
"emails": [{
"email": "",
"type": "",
"primary": false,
"is_selected": true,
"nest_uid": "17_15_1536914882_yhHDzQsDSK", //here
"validation": {
"isError": false,
"inputList": [],
"message": "",
"is_selected": true
},
"checked": false
}],
"phones": [{
"phone": "",
"type": "",
"primary": false,
"is_selected": false,
"nest_uid": "17_15_1536914882_uHN38SxJ3s",
"validation": {
"isError": false,
"inputList": [],
"is_selected": true,
"message": ""
},
"checked": false
}],
"nest_uid": "17_15_1536914882_hwzB7dIn9v",
"checked": false
},
{
"con_title": "",
"con_fName": "",
"con_lName": "",
"con_job_title": "",
"is_selected": false,
"emails": [{
"email": "",
"type": "",
"primary": false,
"is_selected": false,
"nest_uid": "17_15_1536914882_yhHDzQsDSx",
"validation": {
"isError": false,
"is_selected": false,
"inputList": [],
"message": ""
},
"checked": false
}],
"phones": [{
"phone": "",
"type": "",
"primary": false,
"is_selected": false,
"nest_uid": "17_15_1536914882_uHN38SxJ3Y",
"validation": {
"isError": false,
"is_selected": false,
"inputList": [],
"message": ""
},
"checked": false
}],
"nest_uid": "17_15_1536914882_hwzB7dIn9x",
"checked": false
}
]
}
答案 0 :(得分:3)
递归确实是这里的答案。即使不是最漂亮的东西,这样的事情也应该起作用:
function selectInJSON(uid, current) {
current.is_selected = current.nest_uid === uid
Object.values(current).forEach((child) => {
if (Array.isArray(child)) {
child.forEach((item) => {
selectInJSON(uid, item)
if (item.is_selected) current.is_selected = true
})
}
else if (Object(child) === child) {
selectInJSON(uid, child)
if (child.is_selected) current.is_selected = true
}
})
}