我在Array下面。
[{
"text": "test",
"subheader": "Production",
"enabled": false,
"nodes": [{
"text": "test",
"subheader": "JP",
"enabled": false,
"nodes": [{
"text": "test",
"subheader": "test Operations",
"enabled": true,
"nodes": [{
"text": "test",
"subheader": "test Operations",
"enabled": false,
"nodes": [{
"text": "test",
"subheader": "test Operations",
"enabled": true
}]
}]
}]
}, {
"text": "test",
"subheader": "Planning JP",
"enabled": false,
"nodes": [{
"text": "test",
"subheader": "test Operations",
"enabled": false,
"nodes": [{
"text": "test",
"subheader": "test Operations",
"enabled": true
}]
}]
}, {
"text": "test",
"subheader": "india",
"enabled": false
}, {
"text": "test",
"subheader": "Sales Office, South East Japan",
"enabled": false
}, {
"text": "test",
"subheader": "Partner Sales JP",
"enabled": false
}]
}]
如果看到此数组块。
"nodes": [{
"text": "test",
"subheader": "test Operations",
"enabled": true
}]
上面的数组代码块将动态添加,并且可能添加n次。现在,我只想在整个数组中将 enabled true更改为false
有人可以帮助我进行代码循环,并将完整结构中的启用值更改为false吗?
答案 0 :(得分:1)
我知道这是一个很糟糕的问题,但这是将ALL enabled:false更改为true的一种真正简单的方法
let arr = [{ "text": "test", "subheader": "Production", "enabled": false, "nodes": [{ "text": "test", "subheader": "JP", "enabled": false, "nodes": [{ "text": "test", "subheader": "test Operations", "enabled": true, "nodes": [{ "text": "test", "subheader": "test Operations", "enabled": false, "nodes": [{ "text": "test", "subheader": "test Operations", "enabled": true }] }] }] }, { "text": "test", "subheader": "Planning JP", "enabled": false, "nodes": [{ "text": "test", "subheader": "test Operations", "enabled": false, "nodes": [{ "text": "test", "subheader": "test Operations", "enabled": true }] }] }, { "text": "test", "subheader": "india", "enabled": false }, { "text": "test", "subheader": "Sales Office, South East Japan", "enabled": false }, { "text": "test", "subheader": "Partner Sales JP", "enabled": false }] }]
arr = JSON.parse(JSON.stringify(arr).replace(/"enabled"\:false/g,'"enabled":true'))
console.log(arr)
答案 1 :(得分:0)
您可以简单地使用地图功能:
let data={
"nodes": [{
"text": "test",
"subheader": "test Operations",
"enabled": true
}]
}
data.nodes.map((node)=>Object.assign(node,{enabled:false}))