Let say that there is this matrix:
myArray = [["a", 6, 5.54, "b"],
["xxx", 65.5, 45],
[343, "abc", 0.09]];
My goal is to compute the sum of each sub-array but ignoring the last sub-array, it this case [343, "abc", 0.09]. Also, ignoring the string values.
I managed to do it for all the sub-arrays and it looks like this:
myArray = [["a", 6, 5.54, "b"],
["xxx", 65.5, 45],
[343, "abc", 0.09]];
result = myArray.map(a => a.reduce((s, v) => s + (+v || 0), 0));
console.log(result)
Don't know which condition to add in order to ignore the last sub-array.
Any ideas?
.map(), because it's intended to be used to produce a value for every element of the source array. You could use.reduce()instead, and just not change the accumulator in the last call to the callback.reducefunction also takes two additional arguments: the index and the array. That should get you what you need to determine whether you're on the last index.