【发布时间】:2016-11-02 00:50:33
【问题描述】:
我在从对象数组的某个索引中获取值时遇到问题。
数组看起来像这样:
[Object, Object, Object, Object]
当我在 Crome 控制台中打开它们时,它看起来像这样:
问题是我想要来自这个对象之一的uuid 的值。
如果我写 console.log(this.myArray[0].uuid),我会从索引 0 获取 uuid。
但是一旦我写了console.log(this.myArray[index].uuid),其中index 是一个数字,我只会得到undefined。
我已经试过了
var test = _.map(this.myArray,"uuid");
console.log(test[index].uuid)
但那只会是我undefined
有人可以帮帮我吗?
【问题讨论】:
-
我想你想要
test[index],在地图之后。 map 方法将uuid属性返回到新数组中;您将其视为转动对象,每个对象都有一个uuid属性。 -
我刚试过
var test = _.map(test[index](this.mycards,"uuid"));console.log(test);但我只得到swipeToRate.controller.js:189 Uncaught TypeError: Cannot read property '9' of undefined(…) -
这是因为
test[index](this.mycards,"uuid")将test[index]视为一个函数,试图将this.mycards和"uuid"作为参数传递。好吧,实际上这是另一个问题。这似乎是在抱怨test是undefined。
标签: javascript angularjs angular-fullstack