11

I have to loop in a JSON array to get some informations in node, but I only know how to to this using $.each() in jQuery. So I want to know if there are any alternative for the $.each jQuery function in node.js?

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
  • 7
    *"...but I only know how to to this using `$.each()` in jQuery"* That makes me a little sad. You should learn the language. If it's an Array, the closest alternative is `.forEach()`. –  Mar 23 '12 at 00:29
  • Yeah, a year on pure jQuery and starting on node.js makes this things happen. – Nathan Campos Mar 23 '12 at 00:30
  • 2
    By the way, I suggest you take a look at [underscore.js](http://documentcloud.github.com/underscore/) – it's available as a node module, extremely lightweight and provides a myriad of convenience methods such as `each()`, `map()`, `reduce()`, etc. – vzwick Mar 23 '12 at 01:16
  • @squint, `.forEach()` is available only for Array instances, but the person asking wants to iterate over Object with alphabetical keys. – Max Tsepkov Aug 07 '14 at 19:53
  • @MaxTsepkov: I guess I don't see any evidence of that in the question. He says it's an array. But if it was a non-Array object, the point remains since I would hope `for-in` would be among the first things learned. –  Aug 11 '14 at 17:08
  • @squint, you're right – Max Tsepkov Aug 12 '14 at 11:19
  • @vzwick, [lodash](http://lodash.com/) is more advanced drop-in replacement for underscore.js – Max Tsepkov Aug 12 '14 at 11:27

4 Answers4

18

You can use this

for (var name in myobject) {
   console.log(name + ": " + myobject[name]);
}

Where myobject could be your JSON data

Check out the answer here: Looping through JSON with node.js

Community
  • 1
  • 1
Hieu Van Mach
  • 673
  • 3
  • 8
15

You should use the native for ( key in obj ) iteration method:

for ( var key in yourJSONObject ) {
    if ( Object.prototype.hasOwnProperty.call(yourJSONObject, key) ) {
        // do something
        // `key` is obviously the key
        // `yourJSONObject[key]` will give you the value
    }
}

If you're dealing with an array, just use a regular for loop:

for ( var i = 0, l = yourArray.length; i < l; i++ ) {
    // do something
    // `i` will contain the index
    // `yourArray[i]` will have the value
}

Alternatively, you can use the array's native forEach method, which is a tad slower, but more concise:

yourArray.forEach(function (value, index) {
    // Do something
    // Use the arguments supplied. I don't think they need any explanation...
});
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 1
    If you make heavy use of callbacks (and you probably do in nodejs) you may have no choice but to use the forEach method. For example if you make async calls inside the loop and you have to print the outcome of each element in the array, array[i] wouldn't work inside the inner callback. – Jens Feb 17 '14 at 17:33
  • @Joseph Silber, your answer is the correct one, but regarding forEach, the people that might be looking at this will probably need to also know about Object.keys if they are working with an object. – Adam Aug 22 '14 at 15:56
4

In nodejs I found Array.forEach(callback) to best suite my needs. It works just like jQuery:

myItems.forEach(function(item) {
   console.log(item.id);
});

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

jhovgaard
  • 560
  • 4
  • 18
1

jQuery is just javascript, you can do the loop yourself.

I don't know the structure of the JSON array you're looping but you can use a for..in method to get every property of an object.

So you would do something like:

    for( var i = 0; len = jsonArray.length; i < len; i++) {
        for(var prop in jsonArray[i]) {
         //do something with jsonArray[i][prop], you can filter the prototype properties with hasOwnProperty
        }
}

Also you can use the forEach method that Array provides, that works in a similar way that jQuerys .each()

good luck!

nicosantangelo
  • 13,216
  • 3
  • 33
  • 47