push()进入深度数组

时间:2015-06-25 23:40:16

标签: javascript arrays angularjs javascript-objects

尝试推入新的嵌套对象。继续在第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'})

1 个答案:

答案 0 :(得分:5)

你需要像这样的数组访问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作为该数组的属性是undefined。因此,错误

cannot read property push of undefined
相关问题