I have an array in JS and need to look up the data by doing checks against it, but am wondering how i do it =/
My array is structured like this :
//global array spritea
var listObj = {
id: uid,
data: [
abposx,
abposy,
(abposx+imgwidth),
(abposy-imgheight)
]
};
spritea.push(listObj);
This is in a loop so an example of the array is :
spritea = [
{
id: "135",
data: [9,129,345, 687]
},
{
id: "239",
data: [596,382,0,687,33467]
}
];
So what im trying to do is this:
Find
id where x is > data[0] && y > data[1] && x < data[2] && y < data[3]
And then it returns the id ?
Any one know how to do that? Its really confusing me =/
It’s going to be inefficient for large arrays since it’s O(n) (simple linear search), but should do the trick:
It’s untested, but should work. Also, if you need better performance, you’re going to have to index the data somehow.
Edit: jsfiddle: http://jsfiddle.net/mP6cH/3/