Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Push varaibles into an array with containing variables - Javascript

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to set a string as a key of an object you have to use bracket notation

Replace

myArray.push( { name : { fruit : fruitcolor } })

with :

myArray.push( { [name] : { [fruit] : fruitcolor } })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!