I apologize if this has been asked before but I could not find an answer. How do I loop through an array with nested arrays and in the console print out the number of instances an item appears?
So console.log
should print out the number 2 for the name "bob" because "bob" appears twice in the array.
Here is my array and what I have so far:
var names = ["bob", ["steve", "michael", "bob", "chris"]];
function loop(arr, item) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] instanceof Array) {
loop(arr[i], item);
} else {
if (arr[i] == item) {
console.log(arr[i]);
}
}
}
}
loop(names, "bob");