Reputation: 8280
I need help understanding the easiest way to create an array which carries 4 pieces of information and a unique ID tied to it.
My goal is to plot 4 numbers plus an ID which is what im trying to retrieve.
Then the second part of my confusion is finding some way to check the 4 numbers and get the ID. But I'm really confused how to make such an array in JS.
Any tips?
Upvotes: 1
Views: 231
Reputation: 707476
If you want an array of objects and each object has four pieces of data and an id, you would do it like this:
var list = [
{id: "135", data: [9,129,345, 687]},
{id: "239", data: [596,382,0,687,33467]}
];
You would access the data like this:
list[0].id
list[0].data[0]
list[0].data[1]
list[0].data.length
If you want an object that is indexed by id and each id has four pieces of data associated with it, you'd to it like this:
var index = {
"135": [9,129,345, 687],
"299": [596,382,0,687,33467]
};
You would access this data like this:
index["135"][0]
index["135"][1]
index["299"][0]
If you know the ids and want to look data up by id, you would use the second form. If you don't know the ids and just want to be able to easily iterate through the data and see what the ids are and the data is, then you would use the first form.
Upvotes: 1
Reputation: 769
If you thought about an object with numbers, which every has its own id, you can do also that:
var map = {id1:"data1", id2:"data2", id3:"data3", id4:"data4"};
for(x in map) {
alert(x + ": " + map[x]);
}
Upvotes: -1
Reputation: 5924
You would do it like this:
{
"id": [ "data1", "data2", "data3", "data4"]
}
You can construct it like so:
var map = {};
map[id] = [ "data1", "data2", "data3", "data4"]
Upvotes: 2