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

How to get the array element value which has no name in AngularJS

本文关键字:有名称 数组元素 获取 AngularJS      更新时间:2023-09-26

在AngularJS控制器中,我有一个名为contact的关联数组。我需要得到这个没有名字的数组的元素。我在这里张贴了我的代码和回应,供您参考

这是我的控制器代码

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

this my contacts array

[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

this my log of console.log(list) array

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实用程序从联系人数组中获取phonenumber列表。-Just For Info

list.value会给你值

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