我有一个数组:
var data = [{
"name": "Main Hobbies",
"checked": false,
"children": [{
"name": "Dance",
"checked": false,
"children": [{
"name": "Salsa",
"checked": true
}, {
"name": "Solo",
"checked": false
}]
}, {
"name": "Sports",
"checked": false,
"children": [{
"name": "Cricket",
"checked": true
}]
}]
}, {
"name": "Game",
"checked": false,
"children": [{
"name": "PUBG",
"checked": false
}, {
"name": "Cricket",
"checked": false
}, {
"name": "Football",
"checked": true
}]
}]
function removeFalseElem(arr) {
arr.forEach(element => {
if (Array.isArray(element.children) && element.children.length > 0) {
element.children = element.children.filter(e => {
return e.checked === true
});
element.children.map(e => {
delete e['id'], delete e['checked']
})
} else {
removeFalseElem(element.children);
}
delete element['id'], delete element['checked'];
});
}
removeFalseElem(data);
console.log(data);
我如何映射name到key和包含 where is . 应该包含,因为它已被检查。我只想颠倒这个问题的方法childrenarraycheckedtrueMain hobiesSalsa
输出应该像
{
"Main Hobbies": { Dance: ["Salsa"], Sports: ["Cricket"] },
Game: ["Football"]
}
必须只保留checked:true值
繁星coding
慕容3067478
相关分类