[英]How to set specific property value of all objects in a javascript object array (lodash)
[英]How to set specific property value with another property value in a javascript nested object array?
var arr = {
"data": [
{
id : "a1",
guid : "sdfsfd",
value : "abc",
"status": "active"
},
{
id : "a2",
guid : "deaf",
value : "def2",
details : {
"status": "inactive",
"body": done
}
"stat": "inactive"
},
{
id : "a2",
guid : "blind",
value : "def4",
details : {
"status": "inactive",
"body": donenot
}
"stat": "inactive"
},
]
}
console.log(arr.data.filter(item => item.stat === "inactive"))
输出
[{
details: {
body: "done",
status: "inactive"
},
guid: "deaf",
id: "a2",
stat: "inactive",
value: "def2"
}, {
details: {
body: "done",
status: "inactive"
},
guid: "blind",
id: "a3",
stat: "inactive",
value: "def4"
}]
如果我想设置怎么办
为了
arr.data = arr.data.filter(item => item.stat === "inactive").map(e => ({...e, value : e.guid}))
输出
{
data: [{
details: {
body: "done",
status: "inactive"
},
guid: "deaf",
id: "a2",
stat: "inactive",
value: "deaf"
}, {
details: {
body: "done",
status: "inactive"
},
guid: "blind", --> same as value
id: "a3",
stat: "inactive",
value: "blind"
}]
}
为了
arr.data = arr.data.filter(item => item.stat === "inactive").map(e => ({...e, details : {...e.details, status: e.details.body}}))
输出
{
data: [{
details: {
body: "done",
status: "done"
},
guid: "deaf",
id: "a2",
stat: "inactive",
value: "def2"
}, {
details: {
body: "done",
status: "done"
},
guid: "blind",
id: "a3",
stat: "inactive",
value: "def4"
}]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.