我尝试添加新属性,检查兄弟数组是否为空,但无法通过推送实现我想要的效果。
https://jsbin.com/zutiboyaji/edit?html,js,console,output
let x = [{
"name": [{
"first": "m",
"last": "jordan"
}],
"extraObj": [{
"something": "bla bla bla"
}]
}]
const result = x.map(obj => {
if(obj.extraObj.length > 1){
obj.name.extraObj.push({"whatever":"whatever"})
}
return obj
})
console.log(result)
我的循环上面出了什么问题?
答案 0 :(得分:1)
您的if条件失败是因为您正在检查length>1
您应该使用length >=1
。并且没有name
个对象。使用obj.extraObj.push
let x = [{
"name": [{
"first": "m",
"last": "jordan"
}],
"extraObj": [{
"something": "bla bla bla"
}]
}]
const result = x.map(obj => {
if(obj.extraObj.length >= 1){
obj.extraObj.push({"whatever":"whatever"})
}
return obj
})
console.log(result)