admin管理员组文章数量:1185655
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
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
Share Improve this question asked Mar 3, 2012 at 4:42 user1219627user1219627 7876 gold badges11 silver badges17 bronze badges 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? – takteek 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 – user1219627 Commented Mar 3, 2012 at 4:57
5 Answers
Reset to default 18Why 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);
}
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
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.
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
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}`);
}
本文标签: Creating arrays in JavascriptStack Overflow
版权声明:本文标题:Creating arrays in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738310954a2074052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论