0

This is my code:

var Memory ={
 
  personAbove: "someone",
  words: wordsMem = []  <<<<<this thing doesn't work
  
}

how do I make it work? how do I make "words" refrence an array of words inside the "Memory" object?

1

6 Answers 6

1
var Memory = {
  personAbove: "someone",
  words: []
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this one , it might help you

var Memory ={
 
  personAbove: "someone",
  words: { wordsMem : []  }
  
}

Comments

0

You can do simply like this:

var Memory = {
      personAbove: "someone",
      words: [] //Now you can add any value is array
    }

You can parse this array value like this: Memory.words or Memory['words'] whichever you like.

You can read more about array and object here: https://eloquentjavascript.net/04_data.html

Comments

0

You can make array inside an object

var Memory ={
 
  personAbove: "someone",
  words: { 
     wordsMem:["a","b","c"] 
  }
  
}

to access values inside array Memory["words"]["wordsMem][0] //a

or

Memory.words.wordsMem[0] //a

Comments

0

Objects are constructed using key value pairs, so "personAbove" is a key for the value "someone". Similarly an array is a value, so all you need is a key.

let Memory = {
  personAbove: "someone",
  words: []
}

Sets the empty array as the value of Memory.wordsMem. Alternatively, you can define the wordsMem variable first, then reference it as the value of the words key.

let wordsMem = [];
let Memory = {
  personAbove: "someone",
  words: wordsMem
}

This sets wordsMem and Memory.words to point at the same array, so performing array operations on one will update the other.

Comments

0
var defaults = {
 backgroundcolor: '#000',
 color: '#fff',
 weekdays: ['sun','mon','tue','wed','thu','fri','sat']
};

Source: Array inside a JavaScript Object?

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.