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

110
Views
How to convert array to object array with custom key in typescript?

I am learning typescript. It might be a silly question, but I am not able to find the answer online. I would like to convert an array to an object array (values are from the array). For example:

Input:

const array = ["Tom", "Jack", "Rose"]

Expected ouput:

[
  {
    name: "Tom",
    initial: "t",
    year: "2021"
  },
  {
    name: "Jack",
    initial: "j",
    year: "2021"
  },
  {
    name: "Rose",
    initial: "r",
    year: "2021"
  },
]

What is the best way to achieve this in typescript?

Thanks!

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

0

This maybe the easiest way:

const array = ["Tom", "Jack", "C"]

const newObj = [];

array.forEach(eachArrayElement => {
  const x = {
    name: eachArrayElement,
    initial: eachArrayElement[0].toLowerCase(),
    year: (new Date().getFullYear()).toString()
  };
  newObj.push(x);
})

console.log('New Obj ==>', newObj);

about 4 years ago · Juan Pablo Isaza Report

0

Maybe even easier:

const array = ['Tom', 'Jack', 'Rose'];

const arrayOfObjects = array.map(element => {
  return {
    name: element,
    initial: element.charAt(0),
    year: new Date().getFullYear().toString(),
  };
});

console.log(arrayOfObjects);

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!