Access Array Data with Indexes then Modify Array Data With Indexes?

Hi,

I understand this array:

var myArray = [1,2,3];
var myData = myArray[0]; 

→ 1

As 0 is 1, 1 is 2 etc.

BUT…

var myArray = [1, 2, 3];
myArray[0] = 3;

→ 3,2,3

Where does that come from please?

I need to understand that before I’m able to move onto - Access MultiDimensional Arrays With Indexes :dizzy_face:

Nigel

var myArray = [1, 2, 3];
myArray[0] = 3;

First you set the array with 3 values. Then you say: change the first value (index 0) to a value of 3.

So you can acces a value of an element in an array (var myData = myArray[0]; or console.log(myArray[0]);), or change the value of an element in an array (myArray[0] = 3;).

2 Likes

Many thanks. Perhaps I’d lost it in FFC’s explanation somewhere?

NOW… please don’t go away!

I thought, once I knew that, I’d understand the next challenge… wishful thinking!

var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];

var myData = myArray[0][0]; // this is to equal 8

But before replying to your good self, I looked at it again.

And came up with:

var myData = myArray[2][1];

document.write(myArray[2][1]);

→ 8

:slight_smile::slight_smile::slight_smile::slight_smile:

my notes:

Does that look right?

2 Likes

They should include that in the challenge! This is a very clear explanation of nested arrays.

1 Like

Thank you BenGitter https://twitter.com/nigelfrancisnet/status/781478175515836416

Considering I was scratching my head with the first one, which in itself was/is easy… yes the explanation could be better.

Once again, thanks :sunglasses:

@nigelFrancisNET: If you haven’t yet checked out MDN’s JavaScript documentation, you might find that helpful, as well. There’s an indexed collection in their JavaScript Guides on arrays. The whole page is helpful, and if you scroll down, you’ll see a discussion of multi-dimensional arrays with a chart that shows how indexing works. Hope this helps!

Nice … will check it out. Thanks :slight_smile:

1 Like

Thanks for these notes Nigel, this was confusing me until i saw this, now it is much clearer :slight_smile:

1 Like

Nigel, Thank you for the explanation!! Worked for me!!

1 Like