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

206
Visualizações
How to get the value from an array and assign to array of objects?

I am trying to get the values from an array and assign to an array of objects as a new property.
The array of objects:

const employees = [
  {
    firstName: "Ana",
    lastName: "Rosy"
  },
  {
    firstName: "Zion",
    lastName: "Albert"
  },
  {
    firstName: "John",
    lastName: "Doe"
  }
];

An array

const ages = [30, 60, 45]

Desired result

const employees = [
  {
    firstName: "Ana",
    lastName: "Rosy",
    age:30
  },
  {
    firstName: "Zion",
    lastName: "Albert",
    age:60
  },
  {
    firstName: "John",
    lastName: "Doe",
    age:45
  }
];

I tried something like this

const employeesWithAge = (ages) => {
  return employees.map((row) => {
    return {
      ...row,
      age: ages
    };
  });
};
const result = employeesWithAge(ages);
console.log("result", result);

But it adds the entire array of ages to the employees array of objects. Any help will be appreciated

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use Array.prototype.map() for this. Use the spread syntax (...) on the current element to copy the properties into a new object, then add the age property based on the ages array, using .map() callback's second parameter (the current index).

employees.map((employee, i) => ({...employee, age: ages[i]}));

Live example:

const employees = [{
    firstName: "Ana",
    lastName: "Rosy"
  },
  {
    firstName: "Zion",
    lastName: "Albert"
  },
  {
    firstName: "John",
    lastName: "Doe"
  }
];

const ages = [30, 60, 45];

const result = employees.map((employee, i) => ({ ...employee, age: ages[i] }));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think a simple for loop should solve your problem.

for (let i = 0; i < employees.length; i++) {
  employees[i]["age"] = ages[i];
}

This loop iterates through the length of your list of employee objects and adds a new "age" attribute to each object with the corresponding age in your ages array.

All together I imagine your code looking something like this.

const employees = [
  {
    firstName: "Ana",
    lastName: "Rosy"
  },
  {
    firstName: "Zion",
    lastName: "Albert"
  },
  {
    firstName: "John",
    lastName: "Doe"
  }
];

const ages = [30, 60, 45];

for (let i = 0; i < employees.length; i++) {
  employees[i]["age"] = ages[i];
}

console.log(employees);
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