2

I am very new to JQuery, from examples I have managed to create loop, I know it`s simple question but I need your help. How can I convert $.each loop:

$.getJSON('db.json', function(data) 
 {
   $.each(data, function(key, val) 
    {

        if(typeof val === 'object') 
        {
            checkObj(key, val, items);
        } 

    });
}

to for loop? I have tried:

for (var i=0; i< data.length; i++)
    {

        if(typeof val === 'object') 
        {
            checkObj(key, val, items);
        } 

    }

but what to do with key and val?

Daniele
  • 1,938
  • 16
  • 24
Dim
  • 4,527
  • 15
  • 80
  • 139
  • 1
    What does the response look like? – Austin Brunkhorst Aug 11 '13 at 09:10
  • The source code for $.each is very short and pretty readable. It is open source and you could just copy out the loop you need and try to get it to work. To look see the first answer to http://stackoverflow.com/questions/1883611/should-i-use-jquery-each – Paul Aug 11 '13 at 09:17
  • If the top code section is correct, and it might not be, then `items` is defined in previous code, or perhaps undefined. It is not defined in the $.getJSON or the $.each callbacks, which only identify `data`, `key`, and `val` – Paul Aug 11 '13 at 09:21

1 Answers1

7

You're very close (for arrays). Just add:

var val;

at the top and then

val = data[i];

in the loop.

var i, val;
for (i=0; i< data.length; i++)
{
    val = data[i];
    if(typeof val === 'object') 
    {
        checkObj(i, val, items);
        //       ^----- `i`, not `key`, since you used `i` instead of `key` for the loop
    } 

}

But my question would be: Why do that? $.each is very handy, not least because it provides this nice contained scope for the key/i and val variables. If you're worried about making a function call on each iteration, don't be.

$.each also has a dual-nature: When you're giving it an array, it loops through the array elements. But when you give it an object, it loops through the enumerable properties of the object. Both are quite handy.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    One advantage that `for` has over `$.each` is somewhat better performance. – Nicklas Nygren Aug 11 '13 at 09:22
  • 1
    @Nicklas: T.J. addressed this with the sentence *"If you're worried about making a function call on each iteration, [don't be](http://blog.niftysnippets.org/2012/02/foreach-and-runtime-cost.html)."* Though there will be an additional overhead when using `$.each`, not only the function call. – Felix Kling Aug 11 '13 at 09:26
  • 1
    [It's possible to make an `.each` implementation that performs the same as a for loop](http://jsperf.com/for-foreach-jqeach) (at least in V8 and SpiderMonkey due to implementing inlining optimization). jQuery `$.each` however performs like a dog, avoid when performance is a concern. – Esailija Aug 11 '13 at 09:28
  • Yeah. The issue with `$.each` (and `forEach` to an extent) is it tries to be all-singing, all-dancing. If you don't need `thisArg` (and you almost never do) and you aren't dealing with sparse arrays, you can dramatically improve performance. Great perf test, btw. For completeness, I've added "own" and "in" tests, which absolutely kill the performance: http://jsperf.com/for-foreach-jqeach/5 (I also added checks that each loop actually did work; always best to have those checks in there). – T.J. Crowder Aug 11 '13 at 10:05
  • Yes, `.hasOwnProperty` and `in` drastically decrease performance. `typeof ... !== 'undefined'` is a much more efficient existence test (though of course not completely equivalent, but probably sufficient in most cases). See http://jsperf.com/hasownproperty-vs-in/2. – Felix Kling Aug 11 '13 at 17:30
  • @FelixKling: Well, it wouldn't be appropriate for `$.each` or similar. There is a big difference between an entry with an undefined value and the lack of an entry. `hasOwnProperty` didn't surprise me much, but I have to admit the cost of `in` did. – T.J. Crowder Aug 11 '13 at 18:08
  • *"There is a big difference between an entry with an undefined value and the lack of an entry"* That's what I meant with they are not completely equivalent. But I still think that an entry with an undefined value is less common than a missing entry, but of course it depends on the specific situation. In any case, I assume that `hasOwnProperty` and `in` are so slow compared to property access, because property access is heavily optimized. – Felix Kling Aug 11 '13 at 19:10