I am having an array that consists the objects with a key, value how can we iterate each object for caste
and id
.
[
Object {
Use jQuery.each:
$.each([your array of objects], function(index, value) {
// Do what you need, for example...
alert(index + ': ' + value);
});
To iterate over an array filled with stuff in jQuery use $.each, to iterate over an Object for its properties use for..in
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>
var array = [{caste: "Banda", id: 4}, {caste: "Bestha", id:6}];
var length = array.length;
for (var i = 0; i < length; i++) {
var obj = array[i];
var id = obj.id;
var caste = obj.caste;
}
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.
you can use jquery to iterate through all the objects jQuery wants you to fill a callback function, which jquery will call back. The first input parameter will be given the key and the second input parameter the value:
$.each(dataList, function(index, object) {
$.each(object,function(attribute, value){
alert(attribute+': '+value);
});
});
documentation: http://api.jquery.com/jQuery.each/