javascript - 如何获取AngularJS中没有名称的数组元素值

标签 javascript angularjs arrays

在 AngularJS Controller 中,我有一个名为 contact 的关联数组。我必须获取没有名称的数组的元素。在这里我发布了我的代码和回复供您引用

这是我的 Controller 代码

   function onSuccess(contacts) {
               console.log(contacts);
            for (var i = 0; i < contacts.length; i++) {
              var list = contacts[i].phoneNumbers;
               console.log(list);
}
}

这是我的联系人数组

[Contact, Contact, Contact, Contact, Contact, Contact, Contact, Contact]
0:Contact
addresses:null
birthday:Invalid Date
categories:null
displayName:"UIDAI"
emails:null
id:"16"
ims:null
name:Object
nickname:null
note:null
organizations:null
phoneNumbers:Array[1]
0:Object
id:"109"
pref:false
type:"other"
value:"1800-300-1947"
__proto_:Object
length:1
__proto_:Array[0]
photos:null
rawId:"17"
urls:null
__proto__:Object

1:Contact
addresses:null
birthday:Invalid Date
categories:null
displayName:"Distress Number"
emails:null
id:"17"
ims: null
name:Object
nickname: null
note:null
organizations:null
phoneNumbers:Array[1]
0:Object
length:1
__proto__:Array[0]
photos:null
rawId :"16"
urls:null
__proto__:Object

这是我的 console.log(list) 数组日志

Array[8]
0:Array[1]
0:Object
id:"109"
pref:false
type:"other"
value:"1800-300-1947"

在这里我必须从中获取元素value

最佳答案

这里 phoneNumbers 是一个数组。

因此您需要将其访问为 contacts[i].phoneNumbers[0].value

您还可以使用 lodash 实用程序仅从联系人数组中获取电话号码列表。 -仅供引用

关于javascript - 如何获取AngularJS中没有名称的数组元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39164480/

相关文章:

PHP - 如何在函数中使用数组?

ruby - 数组到数组的哈希

javascript - 当表单输入的值无效时,如何防止 AngularJS 解除表单输入值与其模型的绑定(bind)?

javascript - 在 ASP.Net Web API 的情况下从 angularjs http post 响应中检索值

javascript - Angular JS - UI路由器不工作

arrays - 如何将 numpy 数组写入字节内存流?

javascript - hasOwnProperty 在检查父对象属性时返回 true

javascript - 使用 Node.js EventEmitter 而不使用子类化

javascript - 如何删除边框顶部和 div 之间的间距

javascript - 在 Mongoose 查询中使用变量作为属性名称未返回正确结果