我有一个名为nemesis的对象
const nemesis= {
name: 'api_G',
badAss: true,
inventory: [],
health: 10,
item: {type: "SQLinjections", damage: 100,
},
}
我希望从项目对象中将键'type'推送到空库存数组 使用名为:weaponize
的函数 function weaponize(nemesis, item){
let pushItem = nemesis.item.type
let pickItem = nemesis.inventory.push(pushItem);
return pickItem;
}
但调用函数会抛出错误
TypeError:无法读取未定义
的属性“item”答案 0 :(得分:0)
您不需要第二个参数item
,只需将nemesis
对象传递给函数即可:
const nemesis= {
name: 'api_G',
badAss: true,
inventory: [],
health: 10,
item: {type: "SQLinjections", damage: 100,
},
}
function weaponize(nemesis){
let pushItem = nemesis.item.type
nemesis.inventory.push(pushItem);
return nemesis;
}
console.log(weaponize(nemesis));