just looking for a little help. I think I've overcomplicated this. Basically I'm trying to just count the objects in this array filled with different data types. I tried just to separate the objects and count them, but obviously I'm now getting both arrays and objects and counting them. The below outputs 4 when it should be 3.
Any help appreciated, still new to coding.
function countTheObjects(arr) {
let objects = [];
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'object')
objects.push(arr[i]);
}
return objects.length;
}
console.log((countTheObjects([1, [], 3, 4, 5, {}, {}, {}, 'foo'])))
Array.isArray. But what will you do with sets, maps, regexes, dates, ...etc, will they count as objects or not?Array.isArray()to test for “array-ness”.