I am having an array that consists the objects with a key, value how can we iterate each object for caste
and id
.
[ Object { caste = "Banda", id = 4 }, Object { caste = "Bestha", id = 6 } ]
forEach() is an array function from Node. js that is used to iterate over items in a given array. Parameter: This function takes a function (which is to be executed) as a parameter. Return type: The function returns array element after iteration.
Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object. Some objects may contain properties that may be inherited from their prototypes.
Using jQuery.each()
:
var array = [ {caste: "Banda", id: 4}, {caste: "Bestha", id: 6} ]; $.each(array, function( key, value ) { console.log('caste: ' + value.caste + ' | id: ' +value.id); } );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Example code:
var list = [ { caste:"Banda",id:4}, { caste:"Bestha",id:6}, ]; for (var i=0; i<list.length; i++) { console.log(list[i].caste); }
It's just an array, so, iterate over it as always.
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