9

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

2
  • I'm confused about what you're trying to do. Are you trying to store a bunch of (id, value) pairs? Do you need to retrieve the value by id at a later time or just iterate through the pairs? Commented Mar 3, 2012 at 4:49
  • @takteek iterate through the pairs so I want to access both the value and id at element 0 Commented Mar 3, 2012 at 4:57

5 Answers 5

19

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

id and value are not always 12 and 44, its whatever the user sets the var to unless theres a way to set myElement.id = 0 myElement.value then set array to myElement
You just described how to do it, haha. myElement.id = 0
@takteek Awesome thanks...I was hoping it would work the same way
3

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

Comments

1

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.

Comments

0

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

Comments

0

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}`);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.