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

213
Views
Converting arrays nested in array into object using forEch (does not work somehow?)

I am having trouble with something i thought it will be simple.

I have an array of nested arrays with strings.

const cities = [['Vienna'],['Berlin'],['London'],['Oslo'],['New York']]

I must convert these nested arrays into objects. I think forEach method should suit perfectly along with Object.assign.

I written something like these:

function convert(element) {
    Object.assign({}, element)
  }
  const Test = cities.forEach(convert)

But then i am getting from console.log(Test) undefinded. Why so ? I am iterating through the whole array and each of her arrays should be assign as object. Whats missing ?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Object should contain key: value pair

If you want to convert each string element into an object element. Then you can do something like this :

const cities = [['Vienna'],['Berlin'],['London'],['Oslo'],['New York']]

const res = cities.map((item) => {
    return {
    city: item[0]
  }
});

console.log(res);

about 4 years ago · Santiago Trujillo 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!