Reputation: 787
I am little new to javascript and I am having alittle trouble wrapping my head around making a 2d (or maybe i might need a 3d) array in javascript.
I currently have 2 pieces of information i need to collect: an ID and a value so I created the following:
var myArray = [];
var id = 12;
var value = 44;
myArray[id]=value;
But I realized that this is not easy to loop through the array like a for loop so i was thinking of this:
myArray[myArray.length] = id;
myArray[myArray.length-1][id]=value;
I wanted to do this so that in a for loop i could get the ids and the values easily but the above only returns the value when i loop through it. Any suggestions on how to get this working or is there a better way to do this?
Thanks
Upvotes: 9
Views: 33401
Reputation: 1
object in js are more powerful
You can do something like that :
let myArray = [];
myArray.push(
{ id: 1210312, value: 12 }
);
If you want to log each item from this array, you can use this to show in the console each item of your array !
for (let item of myArray) {
console.log(`id: ${item.id}, value: ${item.value}`);
}
Upvotes: 0
Reputation: 11
There is no Associative array in javascript. so if you name a index as named it will be converted as object
so you can create like
var arrayToBe = [];
arrayToBe[]="test";
this way you can add values to that array
Upvotes: 0
Reputation: 409
I think what you want is a dictionary like structure (in JS it's called an object), eg { id => value}
In JS you can do something like this:
var myDict = { 'myId' : 'myValue' };
myDict[12] = 44; // from your example and this will be added to myDict
myDict[11] = 54;
for (var key in myDict) {
console.log('key: ' + key + ', value: ' + myDict[key]);
}
The output will be:
key: 11, value: 54
key: 12, value: 44
key: myId, value: myValue
Upvotes: 3
Reputation: 22128
Probably the best way to do multi-dimensional arrays is creating then on the fly using loops.
so you can have like array[x][y][z]
like I saw your 3D-array
tag.
To loop trough is not that hard, you can verify the length of the array, when you reach the end of one dimension e.g. z
increase the y
by one and reset z
to 0.
Exemple constructor of 2d-array:
function Array2d(x,y){
var array = new Array(x);
while(x>0){
array[x]=new Array(y);
x--;
}
return array;
}
to create a 2d array you can use like this:
var myMatrix = Array2d(4,4);
this is generate a empty 4x4 matrix myMatrix[4][4]
(0,0) (1,0) (2,0) (3,0)
(0,1) (1,1) (2,1) (3,1)
(0,2) (1,2) (2,2) (3,2)
(0,3) (1,3) (2,3) (3,3)
to do a 3D or more dimensions just put other loop inside the loop.
note: I not tested this function, this is just a algorithm, you should verify the input, if you insert a negative length or float this is probably crash you browser.
Upvotes: 1
Reputation: 16510
Why not use an array of object hashes? This approach allows you to store multiple values in a key:value format:
var myArray = [];
var myElement = {
id: 12,
value: 44
}
myArray[0] = myElement;
You could then loop through all of the elements in myArray
like so:
var i = 0,
el;
while (el = myArray[i++]) {
alert(el.id + '=' + el.value);
}
Upvotes: 18