Reputation: 54
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?
Upvotes: 0
Views: 81
Reputation: 403
var defaults = {
backgroundcolor: '#000',
color: '#fff',
weekdays: ['sun','mon','tue','wed','thu','fri','sat']
};
Source: Array inside a JavaScript Object?
Upvotes: 0
Reputation: 1309
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.
Upvotes: 0
Reputation: 1995
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
Upvotes: 0
Reputation: 5054
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
Upvotes: 0
Reputation: 801
try this one , it might help you
var Memory ={
personAbove: "someone",
words: { wordsMem : [] }
}
Upvotes: 0