I'm creating a JSONArray with javascript, and something strange happened to my new Array:
var arrayfinal = new Array();
//Order array by elements
function SortByPuntuation(x,y) {
return x.puntuation - y.puntuation;
}
for (var i = data.length - 1; i >= 0; i--) {
if(arrayfinal){
arrayfinal[i] = arrayfinal.push({'cathegory':data[i].cname , 'puntuation' : 2 * data[i].cnumber + data[i].click});
}
else{
arrayfinal[i] = {'cathegory':data[i].cname , 'puntuation' : 2 * data[i].cnumber + data[i].click };
}
};
arrayfinal = arrayfinal.sort(SortByPuntuation);
The data is another array:
{"cname":"Sillas","cnumber":0,"click":1},
{"cname":"Mesas","cnumber":0,"click":2},
{"cname":"Plastico diferente","cnumber":0,"click":0},
{"cname":"Vasos","cnumber":5,"click":7},
{"cname":"Ordenadores","cnumber":0,"click":2},
{"cname":"Envases cuadrados","cnumber":0,"click":0},
{"cname":"Envases redondos","cnumber":0,"click":0},
{"cname":"Teclados ordenador","cnumber":0,"click":0},
{"cname":"Film transparente","cnumber":0,"click":0},
{"cname":"Pantallas","cnumber":0,"click":1},
{"cname":"Papeles","cnumber":0,"click":2},
{"cname":"Otro","cnumber":0,"click":0},
{"cname":"Coches","cnumber":0,"click":9},
{"cname":"Libros","cnumber":0,"click":2},
{"cname":"Asus","cnumber":0,"click":1}
And the finalarray result is:
[ 29,
{ cathegory: 'Plastico diferente', puntuation: 0 },
{ cathegory: 'Envases cuadrados', puntuation: 0 },
{ cathegory: 'Otro', puntuation: 0 },
{ cathegory: 'Envases redondos', puntuation: 0 },
{ cathegory: 'Film transparente', puntuation: 0 },
{ cathegory: 'Teclados ordenador', puntuation: 0 },
22,
21,
20,
19,
18,
17,
16,
28,
{ cathegory: 'Sillas', puntuation: 1 },
27,
26,
25,
{ cathegory: 'Pantallas', puntuation: 1 },
24,
23,
{ cathegory: 'Papeles', puntuation: 2 },
{ cathegory: 'Ordenadores', puntuation: 2 },
{ cathegory: 'Libros', puntuation: 2 },
{ cathegory: 'Mesas', puntuation: 2 },
{ cathegory: 'Coches', puntuation: 9 },
{ cathegory: 'Vasos', puntuation: 17 },
1 ]
Where do the numbers that appear in the finalarray come from?
It's because you're assigning the result of the push (which is the new length) to arrayfinal[i].
Change
arrayfinal[i] = arrayfinal.push({'cathegory':data[i].cname , 'puntuation' : 2 * data[i].cnumber + data[i].click});
to
arrayfinal.push({'cathegory':data[i].cname , 'puntuation' : 2 * data[i].cnumber + data[i].click});
Your code could normally work by initializing the array, using a forward loop and pushing each element to the array.
var arrayfinal = [];
for(var i = 0; i < data.length; i++) {
arrayfinal.push({
'cathegory':data[i].cname ,
'puntuation' : 2 * data[i].cnumber + data[i].click
});
};
Avoiding the problem you created by mixing up array look up and Array#push:
arrayfinal[i] = arrayfinal.push(...);
which returns the new length of the array after the element is added to it.
Alternatively, you could use Array#map.
var arrayfinal = data.map(function(category){
return {
'cathegory': category.cname,
'puntuation': 2 * category.cnumber + category.click
};
});