Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have a JSON array which is returned from my PHP ajax call. I need to loop through it and display all the information. I can not seem to get a method working. I am used to using the foreach key => value, in PHP but that doesn't seem to be an option here.

my array is

[{"Type":"Person","Durable":"Durable","Url":"test.com"},
{"Type":"Person","Durable":"Durable","Url":"test2.com"},
{"Type":"Person","Durable":"Durable","Url":"test3.com"},
{"Type":"Person","Durable":"Durable","Url":"test4.com"},
{"Type":"Location","Durable":"Durable","Url":"test5.com"},
{"Type":"Phone","Durable":"Durable","Url":"test6.com"}]

The length of the array changes every time it is not always 6 items. And the loop is going to go in my success handler. I just need help on how to access the data.

success: function(data){

}
share|improve this question
1  
for (var i in data) { ... } should work. You would reference an array item with data[i] – Joseph Marikle Feb 4 at 20:53
    
I know that it is, but everywhere else I saw I found crazy amount of different ways and i couldnt get any to work – workingxx Feb 4 at 20:58

3 Answers 3

up vote 1 down vote accepted

You can use a simple loop statement:

   success: function(data){
    var i, l;
    for (i = 0, l = data.length; i < l; i++) { 
        // access the object: data[i]
        console.log(data[i]);
    }
  }

This is the most effiecient way.

share|improve this answer
    
This is the most effiecient way - based on what? – LcSalazar Feb 4 at 20:54
    
And why do you use two variables in your for loop? – LcSalazar Feb 4 at 20:55
    
l is defined only once. If you done something like data.length withn the evaulation, it is computed each time – skay- Feb 4 at 20:56
    
What if i needed to display the data in a special location. How could i access it like data[Type] as a variable – workingxx Feb 4 at 21:09
    
you just do data[i].Type .. data[i].Durable .. etc.. This solution is a bit faster than forEach and also works on older browsers. – skay- Feb 4 at 21:11

You can just loop through the array:

success: function(data){
  for (var i = 0; i < data.length; i++) { 
    var obj = data[i];
    var type = obj.Type;
    var durable = obj.Durable;
    var url = obj.Url;
    // do work
  }
}
share|improve this answer

You can use the array prototype forEach:

data.forEach(function(item) {
    var type = item["Type"];
    var durable = item["Durable"];
    /*...*/
});
share|improve this answer
    
The downvoter care to explain? – LcSalazar Feb 4 at 20:59
    
forEach is not supported natively on some older browsers like IE8 – skay- Feb 4 at 21:01
    
This is what I need to access the data how i want, thank you so much! – workingxx Feb 4 at 21:03
    
Ahh really thats not good – workingxx Feb 4 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.