Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
Is there a cleaner way to add a new property with unique values to an array

I'm just learning js now (late to the party I know) and I have the following code and I was wondering if it could be written in a cleaner/simpler way?

Also, ideally, instead of using "if (obj.id === 1)" I would like to iterate through the array and add age based on the sequence i.e. [0] would become '32' and so on.

const students = [ // Three objects, each with four properties
 {
  id: 1,
  name: 'Mark',
  profession: 'Developer',
  skill: 'JavaScript'
 },
 {
  id: 2,
  name: 'Ariel',
  profession: 'Developer',
  skill: 'HTML'
 },
 {
  id: 3,
  name: 'Jason',
  profession: 'Designer',
  skill: 'CSS'
 },
];

const studentsWithAge = students.map(obj => {
if (obj.id === 1) {
 return {...obj, age: '32'};
 } else if (obj.id === 2) {
  return {...obj, age: '26'};
 } else if (obj.id === 3) {
  return {...obj, age: '28'};
 }
 return obj;
});

console.log(studentsWithAge);

// output
// [
//  {
//   id: 1,
//   name: 'Mark',
//   profession: 'Developer',
//   skill: 'JavaScript',
//   age: '32'
//  },
//  {
//   id: 2,
//   name: 'Ariel',
//   profession: 'Developer',
//   skill: 'HTML',
//   age: '26'
//  },
//  {
//   id: 3,
//   name: 'Jason',
//   profession: 'Designer',
//   skill: 'CSS',
//   age: '28'
//  }
// ]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can map the array into the object like so:

const ages = ['32', '26', '28'];
const studentsWithAge = students.map(obj => { ...obj, age: ages[obj.id-1] });
about 4 years ago · Juan Pablo Isaza Relatório

0

You could create an ages array and use the index to map the value to the corresponding object.

const students = [ // Three objects, each with four properties
  {
    id: 1,
    name: 'Mark',
    profession: 'Developer',
    skill: 'JavaScript'
  },
  {
    id: 2,
    name: 'Ariel',
    profession: 'Developer',
    skill: 'HTML'
  },
  {
    id: 3,
    name: 'Jason',
    profession: 'Designer',
    skill: 'CSS'
  },
];

const ages = [32, 26, 28];

const result = students.map((s, i) => {
  return { ...s, age: ages[i] }
});

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Your code is true, another way to add ages by the id is the following code. Just use ids as object key and age as value.

The following code check if id exists in the ages const then add it to the studentsWithAge. It works exactly like your code.

const ages = {1: '32', 2: '26', 3: '28'};

const studentsWithAge = students.map(obj => { 
    if(ages[obj.id]) obj.age = ages[obj.id]; 
    return obj;
});

But if you're sure all ids have age value simpler code like this could be used:

const ages = {1: '32', 2: '26', 3: '28'};
const studentsWithAge = students.map(obj => ({...obj, age: ages[obj.id]}));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda