About objects and arrays

how to access one entree in an array if the array is inside an object?

var myObj = {
myArray : [1,2,3]

how to get the value 1 for example?
thank you.

In your case, the array itself lives at myObj.myArray - given that as the arrays name, how might you access indexes in that array?


You could review this lesson on Accessing Object Properties with Dot Notation

Then you can review the lesson on Access Array Data with Indexes

Reviewing those two lessons will give you the answer you are looking for.

1 Like

I tried myObj.myArray[n] it worked and i tried to assign myObj.myArray to a variable , then console.loged variable[n] and it worked too. Thank you.

1 Like

Very well done! So doing this:

const myObj = {
  myArray: ["foo", "bar", "baz"]

let theArr = myObj.myArray:

They both point to the same thing! This is because we assign a reference in the object, and we then assign that memory location reference in the variable. So they both see the same place in memory, and if one changes that array, both see it.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.