Reputation: 709
I have a for loop like this:
var mappedPoints = new Array();
for (i=0; i<livePoints.length; i++){
var xOptions = [];
var useX;
var useY;
for (j=0; j<madeMapPoints.length; j++){
if(parseFloat(livePoints[i].Latitude) < parseFloat(madeMapPoints[i].xpoint)){
break;
}
xOptions.push(madeMapPoints[i]);
useX = parseFloat(madeMapPoints[i].xpoint);
}
for(k=0;k<xOptions.length;k++){
if(parseFloat(livePoints[i].Longitude) < parseFloat(xOptions[k].ypoint)){
break;
}
useY = parseFloat(xOptions[k].ypoint);
}
var num = Math.pow(parseInt(useX+useY),2);
mappedPoints[num].push(livePoints[i]);
}
Around the bottom there's the line that says mappedPoints[num].push(livePoints[i]);
. What I want to have happen is push to the index I'm defining in num so if the same value comes up in NUM again, it will be pushed to the array key defined in num.
What I have doesn't work. The PHP equivalent do what I want would be something like this:
$mappedPoints[$useX."_".$useY][] = livePoints[i];
Could I accomplish the same result with JS somehow?
Upvotes: 1
Views: 104
Reputation: 679
You should check if mappedPoints[num]
is an array first.
But I think in this case what you need for mappedPoints
is not an array, you need an object.
In javascript, associative arrays don't exist, but they can be simulated with an object. It's a bit confusing, because both can be accessed the same way.
At the beggining:
var mappedPoints = {}; //this way you define mappedPoints to be an object
At the end:
if(!mappedPoints.hasOwnProperty(num))
mappedPoints[num] = []; //this way you define mappedPoints[num] to be an array
mappedPoints[num].push(livePoints[i]);
Upvotes: 2