gpt4 book ai didi

javascript - 将()插入深层数组

转载 作者:行者123 更新时间:2023-11-28 13:23:43 25 4
gpt4 key购买 nike

尝试插入新的嵌套对象。在第 3 行继续出现 cannot read property push of undefined

为什么这不起作用?我应该以另一种方式做这件事吗?

$scope.item.deliverables[0].steps[0].versions = [];
$scope.item.deliverables[0].steps[0].versions.push({assets:[{url:'aaa'}]})
$scope.item.deliverables[0].steps[0].versions.assets.push({url:'bbb'})

最佳答案

您需要以数组形式访问 versions 本身,如下所示:

$scope.item.deliverables[0].steps[0].versions = [];
$scope.item.deliverables[0].steps[0].versions.push({assets:[{url:'aaa'}]})
// the item you just pushed in the array is in .versions[0] now
$scope.item.deliverables[0].steps[0].versions[0].assets.push({url:'bbb'})

自从您执行此操作后:

$scope.item.deliverables[0].steps[0].versions = [];

versions 是一个数组,而 assets 作为该数组的属性是未定义。因此,错误

cannot read property push of undefined

关于javascript - 将()插入深层数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31062586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com