I have an example of nested array:
var testArray = [1,2,[3,4,[5,6],7],8,9,[10,11],12];
Here is my function for getting nested array length:
Array.prototype.getLength = function() {
var sum = 0;
function getMultiLength(array) {
for (count = 0; count < array.length; count ++) {
sum ++;
if (!array[count].length) {
getMultiLength(array[count]);
}
}
}
getMultiLength(this.valueOf());
return sum;
};
My expectation for result would be 12, but instead what I got is infinite loop:
testArray.getLength(); //infinite loop
Anyone know why and how to get nested array length?