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

92
Views
How to return to new array from Array of elements?

I have an array of elements I want to map to a new array of objects with a key as a name.

let array = ["abc", "def", "xyx"]

Expected Output

let array1 = [{name: "abc"}, {name: "def"}, {name: "xyz"}]
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You can use array.map:

let newArray = array1.map((value) => ({name: value});
about 4 years ago · Santiago Gelvez Report

0

let arr = ['a', 'b', 'c'];

let newArr = arr.map(el => ({ name: el }));

console.log(newArr);

about 4 years ago · Santiago Gelvez Report

0

you can use map() function for that,

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

    let array = ["abc", "def", "xyx"]
    const array1 = array.map( data => { 
    return {name:data}
    })
    /* this code will also work,if you dint specify the properties name 
    by default it will be the variable's name. In this case 'name' */
     const array2 = array.map( name => { 
    return {name}
    })
    console.log(array1)
    console.log(array2)

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