1

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.

8
  • 1
    this.lengthOfMultiArray(array[i], temp); there is no this around here, unless you execute that in the global context in non-strict mode, in which it happens to be window. Also, you execute this recursively but never capture the value or anything. Commented Jan 10, 2020 at 17:32
  • You need to add the result of the recursive call to your final result. Commented Jan 10, 2020 at 17:32
  • And are you really using default arguments? Or is that just to illustrate what you are doing here in your question? If the later, you should just show an example function call instead. Commented Jan 10, 2020 at 17:33
  • You are trying to pass by ref, but it is a primitive datatype and is passed by val. Commented Jan 10, 2020 at 17:38
  • Just so you know why this doesn't work, 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. Commented Jan 10, 2020 at 17:41

3 Answers 3

2

You need to add the result of each recursive call to your total. Also, you don't need the temp parameter, just a local variable to calculate the total:

function lengthOfMultiArray(array) {
    var total = 0;
    for(var i in array) {
        if (Array.isArray(array[i])){
            total += lengthOfMultiArray(array[i]);
        } else {
            total++;
        }
    }

    return total;
}

console.log(lengthOfMultiArray([ [1, 2], [3, 4, 5], [6, 7, 8, 9] ]));

Sign up to request clarification or add additional context in comments.

1 Comment

Removed the disclaimer since now you can test it and it works :P
1

Try the following:

const array = [[1, 2],[3, 4, 5], [6, 7, 8, 9]];
array.flat(Infinity).length;

1 Comment

Please explain why this answer is not acceptable for how to count length of an associate array in JavaScript?.
0

Another simple recursive solution looks like this:

const sum = (ns) => 
  ns .reduce ((a, b) => a + b, 0)

const lengthOfMultiArray = (arr) => 
  Array .isArray (arr) ? sum (arr .map (lengthOfMultiArray)) : 1

console .log (lengthOfMultiArray ([[1, 2], [3, 4, 5], [6, [7, 8], 9]]))

This simply says that the length of a non-array is 1 and the length of an array is the sum of the lengths of its elements.

You might choose to inline the function sum here if you had no other use for it:

const len = (arr) => 
  Array .isArray (arr)
    ? arr .map (len) .reduce ((a, b) => a + b)
    : 1

Or, more to my liking, you might break that down even further, introducing an add function:

const add = (a, b) => a + b
const sum = (ns) => ns. reduce (add, 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.