push() 进入深层数组

push() into deep array

正在尝试推入新的嵌套对象。继续在第 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 作为该数组的 属性 是 undefined。因此,错误

cannot read property push of undefined