push() into deep array

143 Views Asked by At

Trying to push in new nested objects. Keep getting cannot read property push of undefined on line 3.

Why isn't this working? Should I be doing this another way?

$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

There are 1 best solutions below

4
On BEST ANSWER

You need to access versions itself as an array like this:

$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'})

Since you did this:

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

versions is an array and assets as a property to that array is undefined. Hence, the error

cannot read property push of undefined