6

I have been using $.each of the jQuery framework to iterate through a JSON string that I receive via an AJAX call. Now this string is sometimes quite huge and as a result IE6/7/8 crawl as a result.

I am wondering if there is a faster way to iterate through the entire data.

Thank you for your time.

Alec Smart
  • 94,115
  • 39
  • 120
  • 184
  • How are you iterating through it? Are you parsing it, or are you evaling it? Do you need just certain parts? – cgp Jun 26 '09 at 19:02
  • some good clientside parsing & performance tips from [flickr](http://code.flickr.com/blog/2009/03/18/building-fast-client-side-searches/) – redsquare Jun 26 '09 at 19:09
  • What is the structure of this json..? just a huge object or a huge array of objects..? – T J Jan 17 '16 at 06:09

2 Answers2

1

How about using the regular javascript functions?

If for example you have a JSON object with items in them, you could just eval the JSON string to convert it to javascript objects, and iterate over them using 'for (i in object)'.

ylebre
  • 3,100
  • 1
  • 18
  • 14
  • I assume Alec is using the json dataType in the xhr call therefore jquery will be eval'ing the response. – redsquare Jun 26 '09 at 19:10
1

Hope to be still in time!

How about a simple -for-?

for(i = 0; i < data.length; i++) {
    data[i].property = 'todo';
}

Otherwise -for in-

var mycars = [{name:'Ferrari'}, {name:'BMW'}];
for (i in mycars)
{
    document.write(mycars[i].name + "<br />");
}

Here is the complete answer: How do I iterate over a JSON structure?

IvanF.
  • 513
  • 10
  • 18