8

I'm confused about how to create and access 2-dimensional arrays in javascript. Below is an array declaration in which I'm storing names of people and then the src for their image. When I try to access myArray[0][0] element I get 'D' and when I try to access myArray[0,0], I get Donald Duck. How can I access the img src myArray[0][0] = "assets/scrybe.jpg" ?

JS code:

var myArray = new Array(1);

myArray[0] = "Donald Duck";
myArray[1] = "Winnie Pooh";
myArray[2] = "Komal Waseem";
myArray[3] = "Hockey";
myArray[4] = "Basketball";
myArray[5] = "Shooting";
myArray[6] = "Mickey Mouse";

myArray[0][0] = "assets/scrybe.jpg";
myArray[1][0] = "assets/scrybe.jpg";
myArray[2][0] = "assets/scrybe.jpg";
myArray[3][0] = "assets/scrybe.jpg";
myArray[4][0] = "assets/scrybe.jpg";
myArray[5][0] = "assets/scrybe.jpg";
myArray[6][0] = "assets/scrybe.jpg";
2
  • 1
    I don't think you want a 2-dimensional array. I think you want an array of objects, such that each object has a "name" and an "image" property. Commented Sep 29, 2012 at 12:58
  • What you are actually doing is "Donald Duck"[0] = "assets/scrybe.jpg" - which obviously can't work Commented Sep 29, 2012 at 13:03

4 Answers 4

16

Firstly, to create an array, it's better to use the square bracket notation ( [] ):

var myArray = [];

This is the way you emulate a multi-demensional array in JavaScript. It's one or more arrays inside an array.

var myArray = [
    [], [] // two arrays
];

If you're asking if there's a way to declare an array as multi-dimensional, then no, there isn't. You can access them like this:

myArray[0][0]; // the 1st element of the first array in myArray
myArray[1][1]; // the 2nd element of the second array in myArray

Here is the code you were probably looking for:

var myArray = [
    ["Donald Duck", "assets/scrybe.jpg"],
    ["Winnie Pooh", "assets/scrybe.jpg"],
    ["Komal Waseem", "assets/scrybe.jpg"]
    [/* and so on...*/]
];

But since you're giving all the names the same URL, then you can use a for loop instead to do this faster:

for (var i = 0; i < myArray.length; i++) {
    myArray[i][1]  = "assets/scrybe.jpg";
}
Sign up to request clarification or add additional context in comments.

1 Comment

@0x499602D2 any chance you tell us how to access the array length or size or other options. I created a dimensional array as you suggested. However I can not access the length of the array. Any suggestion? var GenreArray#counter# = [ [], [] ]; <cfoutput> <cfset i = i + 1> GenreArray#counter#[#i#] = "#Title#"; for (var i = 0; i < myArray.length; i++) { <cfset counterForTime = counterForTime + 1> GenreArray#counter#[#i#][#counterForTime#] = "#SuitableTime#"; } </cfoutput> </cfoutput>
7

Perhaps what you really want is an array of objects:

var myArrray = [
  { name: "Donald Duck", image: "assets/scrybe.jpg" },
  { name: "Winnie Pooh", image: "assets/scrybe.jpg" },
  // ...
];

JavaScript doesn't really have 2-dimensional arrays, as such. What you can do is create an array such that each element is also an array.

var twoDim = [ [], [], [], [], [] ];

In your case, however, I don't think that structure will help much.

3 Comments

Fair enough answer with an array of objects but no answer how to access them.
@JackTheKnife ?? you access the array elements the same way you access any array element, with the [ ] operator.
To access the a multi dim array with a for-loop myArrray.forEach((item) => { let n = item.name; let i = item.image; ... });
0

No it cannot be done like that. Must either do:

var myArray = new Array(1);

myArray[0] = new Array(1);

myArray[0][0] = "Donald Duck";
myArray[0][1] = "assets/scrybe.jpg";

Or use JS syntax

var myJSObj = {"Donald" :"assets/scrybe.jpg", "Donald2": "assets/scrybe2.jpg" };

Comments

0

I had the same issues and I used minBy from Lodash

https://lodash.com/docs/4.17.4#minBy

var objects = [{ 'n': 1 }, { 'n': 2 }];

_.minBy(objects, function(o) { return o.n; });
// => { 'n': 1 }

// The `_.property` iteratee shorthand.
_.minBy(objects, 'n');
// => { 'n': 1 }

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.