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

166
Views
How to convert array to object with add same key in this object Javascript?

I am trying to convert this array to object with same custom key

let array=['a','b','c']
 let y=    Object.assign({}, [...array]);

I want resualt like this

[{'name':'a'},{'name':'b'},{'name':'c'}]

how I can do this ?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use Array.map. Here it is.

let array=['a','b','c']
const res = array.map(item => ({name: item}))
console.log(res)

about 4 years ago · Santiago Trujillo Report

0

For example. With map() or loops like for... or forEach you can do it.

array=['a','b','c']

let res1 = array.map(a => ({name: a}))
console.log('map()', res1)

// or forEach

let res2 = [];
array.forEach(a => {
  res2.push( {name: a})
})

console.log('forEach()', res2)

about 4 years ago · Santiago Trujillo Report

0

Why haven't you tried a simple for loop yet:

let array=['a','b','c']
let ans = []; //initialize empty array
for(let i = 0; i < array.length; i++){ //iterate over initial array
ans.push({name : array[i]}); //push in desired format 
}
console.log(ans);

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!