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?
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
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...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With