I have associate an array as
[[1, 2],[3, 4, 5], [6, 7, 8, 9]];
i want to count length of an array of this after flattered this array my code is following
function lengthOfMultiArray(array = [ [1, 2], [3, 4, 5], [6, 7, 8, 9] ], temp = 0) {
for (var i in array) {
if (Array.isArray(array[i])) {
this.lengthOfMultiArray(array[i], temp);
} else {
temp++;
}
}
return temp;
}
console.log(lengthOfMultiArray());
and i am using recursive function to count it but for start of every loop it update temp as 0 and start again with zero.
this.lengthOfMultiArray(array[i], temp);there is nothisaround here, unless you execute that in the global context in non-strict mode, in which it happens to bewindow. Also, you execute this recursively but never capture the value or anything.this.lengthOfMultiArray(array[i], temp);-- you're passing temp into the function and expecting the function to change temp, but number variables are immutable -- a function cannot change a number variable, it can only return a new number.