From this foreach I get different lines (I have commented how I tried, but nothing):
respuesta2.forEach( function(valor2){
//resultados_f2[valor2.ronda].push([parseInt(valor2.victorias_1), parseInt(valor2.victorias_2)]);
console.log("-"+valor2.ronda+"-> "+parseInt(valor2.victorias_1)+" "+parseInt(valor2.victorias_2));
});
The result of the previous console is the following:
-2-> 1 0
-1-> 1 2
-1-> 2 1
The array I want to get is the following:
var resultados_f2 = [
[[2,1], [1,2]],
[[1,2]]
];
How could I do it? I want all the lines that have the same valor2.ronda
to be in an array within the array resultados_f2
.
You can do it with the method
reduce
.reduce
will iterate over the array generating a new value based on the current element of the iteration and an accumulated value.In your case, the accumulated value is an array (what you want as the result).
With the following set of values, the result would be as follows.
If you look closely, there are positions in the array that have no value because there was no one with the property
ronda
with the value of that position.Personally, since this can happen, I would choose to output an object , it would be cleaner, but it's up to you.
With the same data set as before, this is the result,
I hope it works.