Reputation: 125
The array is somewhat like this :
[[Object { button={...}},
Object { input={...}},
Object { checkbox={...}},
Object { textarea={...}}],
[Object { textarea={...}}]
]
In the curly brackets i have set some properties like color,value,type etc.
What i want is to get the each object of an array and check through the properties like type of an object and then call a function to perform further things. Just like in PHP we use :
foreach($a as $b){
// and then do something here ..
};
Kindly help me through and i hope everyone can understand what i am trying to say.
// var i is the counter for page numbers
function pagination(i) {
alert(i);
i--;
//page is array
var result = page;
//console.log(result[i]);
var $currentElem;
$(result[i]).each(function() {
currentElem = $(this);
console.log(currentElem);
});
}
Upvotes: 2
Views: 17333
Reputation: 780724
.each
is used when you're looping over the elements of a jQuery collection. To loop over the contents of an array or object. use $.each()
:
$.each(result[i], function(n, currentElem) {
console.log(currentElem);
});
And you shouldn't use $(this)
unless this
is a DOM element. If it's just a Javascript object, wrapping it in a jQuery object is unnecessary.
You can access the properties using the normal Javascript variable.propertyname
syntax, e.g. currentElem.button
and currentElem.button.color
. To append elements to your view, you can do something like:
var button = currentElem.button;
$("<button>", {
value: button.value,
name: button.name,
css: {
color: button.color,
width: button.width,
backgroundColor: button.backgroundcolor
}
}).appendTo($("#buttonDiv");
Upvotes: 4
Reputation: 22158
To iterate you can use for(in) or in jquery $.each.
for(var i in array) {
console.log(array[i]);
}
Upvotes: 1