-1
I need to reduce a array
, adding the values (I was able to do the code below), now with this reduction I need to do a kind of a SORT
to then catch the 3 biggest and the others I’ll make like an if for others
I don’t know if you have one lib
in nodejs
that would facilitate.
- REDUCE
SORT BY THE THREE LARGEST
var array=[ { "paymentId" : 1, "value" : 290 }, { "paymentId" : 2, "value" : 223 }, { "paymentId" : 1, "value" : 2333 }, { "paymentId" : 3, "value" : 24432 }, { "paymentId" : 4, "value" : 23424 }, { "paymentId" : 5, "value" : 2323 }, ] var r = array.reduce(function(pv, cv) { if ( pv[cv.paymentId] ) { pv[cv.paymentId] += cv.value; } else { pv[cv.paymentId] = cv.value; } return pv; }, {}); console.log(r);
EXAMPLE OF 0 RESULT FOR OTHERS
[
{
"paymentId" : 3,
"value" : 24423
},
{
"paymentId" : 4,
"value" : 23424
},
{
"paymentId" : 1,
"value" : 2623
},
{
"paymentId" : 0,
"value" : 2222
},
]
Great, thank you very much for the friendly reply
– Djnalda Araujo da Silva
What better way would you use to find the first 3 and put the rest at the same value as others ? Example

[ { paymentId: 3, value: 24432 },
 { paymentId: 4, value: 23424 },
 { paymentId: 1, value: 2623 },
 { paymentId: 0, value: 2546 },

Where 0 would be the sum of the rest(others)– Djnalda Araujo da Silva