Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda