I have a code
var myArray = []
myArray.push( { "bob" : { "banana" : "yellow" } })
console.log(myArray)
which returns
{
"bob": {
"banana": "yellow"
}
}
Now, I want to change the variables like this:
var myArray = []
var name = "bob"
var fruit = "banana"
var fruitcolor = "yellow"
myArray.push( { name : { fruit : fruitcolor } })
console.log(myArray)
but it doesn't return the same result. How do I fix this?
Thanks!
myArray.push( { [name] : { [fruit] : fruitcolor } }). Use bracket notation.