I have an array like this:
[{"this":"that","int":5},{"this":"that","int":5}]
How can I count the number of objects({}) inside of an array with Javascript?
Thanks in advance.
I have an array like this:
[{"this":"that","int":5},{"this":"that","int":5}]
How can I count the number of objects({}) inside of an array with Javascript?
Thanks in advance.
[{"this":"that","int":5},{"this":"that","int":5}].length; // 2
Try,
var cnt = 0;
var arr = [5 , 3 , "not an object" , {"this":"that","int":5},{"this":"that","int":5}];
arr.forEach(function(itm){
if(!itm.__proto__.__proto__){
cnt++;
}
});
console.log(cnt + "normal objects are there"); //2
Object {} inside array [] treated as items so you find the length of array to count the item(s)
var $data=[{"this":"that","int":5},{"this":"that","int":5}];
var count=$data.length