1
Some time ago I had to execute a sum of arrays with javascript, Example:
allData[0].likes = [5,10,15,20,25];
allData[1].likes = [10,2,3,17,15];
Using a solution I found on the Internet I used the following code modified by myself:
var result = allData.reduce(function(array1, array2) {
return array2["likes"].map(function(value, index) {
return parseInt(value) + (parseInt(array1[index]) || 0);
}, 0);
}, []);
And with this it always returned a single array, in this example the value of "result" would be: [15,12,18,37,40]
It was working perfectly until this month when in the following case below it returns me only a value instead of an array.
allData[0].likes = [8,10,15,20,25];
allData[1].likes = [20];
In this case it returns only the 28 value and ignores the rest of the values
How do I change my code so it doesn’t happen?
in that
array2[likes]
thelikes
should be as string, right? this wayarray2["likes"]
.– Cmte Cardeal
This I had to delete a lot of data for the sake of company confidentiality but yes it is like that, already arranged in Edit.
– Gabriel Almeida