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?

orenog
  • 11
  • 3
  • Does this answer your question? [Array inside a JavaScript Object?](https://stackoverflow.com/questions/1828924/array-inside-a-javascript-object) – Heretic Monkey Jun 22 '20 at 18:54

6 Answers6

1
var Memory = {
  personAbove: "someone",
  words: []
}
habiiev
  • 405
  • 2
  • 9
0

try this one , it might help you

var Memory ={
 
  personAbove: "someone",
  words: { wordsMem : []  }
  
}
Ehsan Nazeri
  • 717
  • 1
  • 5
  • 9
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

Shubham Verma
  • 4,389
  • 1
  • 7
  • 21
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

xMayank
  • 1,737
  • 2
  • 4
  • 18
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.

Charlie Bamford
  • 1,183
  • 3
  • 16
0
var defaults = {
 backgroundcolor: '#000',
 color: '#fff',
 weekdays: ['sun','mon','tue','wed','thu','fri','sat']
};

Source: Array inside a JavaScript Object?

Krishan Kumar
  • 385
  • 3
  • 13