【问题标题】:push() into deep arraypush() 进入深度数组
【发布时间】:2015-06-25 23:40:15
【问题描述】:

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

【问题讨论】:

  • 哇!需要 NSFW 标记
  • 如果您仍然遇到问题,请告诉我,以便我提供帮助

标签: javascript arrays angularjs javascript-objects


【解决方案1】:

您需要像这样以数组的形式访问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

【讨论】:

  • 您解释了问题,但没有提供实际解决方案的代码。
  • @jfriend00,第一个代码sn-p不提供解决方案吗?
  • @jfriend00,将不胜感激。谢谢
  • @jfriend00,漂亮。再次感谢
  • 好的,编辑提供澄清,因为我对你的答案的呈现感到困惑。
猜你喜欢
  • 2021-04-17
  • 2021-11-05
  • 2020-10-22
  • 2021-10-28
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
相关资源
最近更新 更多