I primarily work with C#, and when working with an array of arrays, I can sum the length of the sub arrays using Linq:
var myArray = new int[2][] {
new int[3] { 1, 2, 3 },
new int[4] { 1, 2, 3, 4 }
};
myArray.Sum(s => s.Length);
Printing the result of that sum to a console should produce a 7
. With that in mind, I'm having trouble finding resources on a simple way to do this in JavaScript, and as such I'm stuck doing it with nested for
iterators:
let containedLength = 0;
let myArray = [ [1, 2, 3], [1, 2, 3, 4] ];
for (let x = 0; x < myArray.length; x++)
for (let y = 0; y < myArray[x].length; y++)
containedLength++;
I could write a function to do this, but that would be over the top for what I'm trying to do today since I already have a basic way to do it. Is there a simpler way similar to C#'s Linq that could get the sum of the lengths of arrays, within an array of arrays?
myArray.map(a => a.length).reduce((a, b) => a + b, 0)
\$\endgroup\$myArray.reduce((a, s) => a + s.length, 0)
\$\endgroup\$