0

In AngularJS controller, I am having the associative array called contact. I have to get the elements of the array which has no name. here I have posted my code and response for your reference

this is my controller code

   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"

In here I have to get the element value from this.

5
  • 1
    contacts[i].phoneNumbers.value ? Commented Aug 26, 2016 at 10:50
  • i tried but it shows cannot read property of "value" undefined Commented Aug 26, 2016 at 10:51
  • 1
    ups didn't see, phoneNumbers is an array so contacts[i].phoneNumbers[0].value you may test if phoNumbers is defined and is not an empty array to return an empty string if so. Commented Aug 26, 2016 at 10:53
  • i will try it and tell you thank you budy Commented Aug 26, 2016 at 10:56
  • it works thank you budy Commented Aug 26, 2016 at 11:01

2 Answers 2

1

Here phoneNumbers is an array.

So you need to access it as contacts[i].phoneNumbers[0].value

You can also use lodash utility to get list of phoneNumbers only from contacts array. -Just For Info

Sign up to request clarification or add additional context in comments.

Comments

0

list.value will give you the 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;
          }
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.