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

149
Visualizações
Field value from first Array to get the values from the Second Array in Javascript

I've got two arrays, arr1 has the field and I need to take only the field value from it. The field value should be checked with the arr2 and if it matches the name of the key then need to create as the output below:

let arr1 = [{
      field: "name",
      value: "some1",
      value1: "some2"
    },{
      field: "job",
      value: "some1",
      value1: "some2"
    },{
      field: "from",
      value: "some1",
      value1: "some3"
    }
    ];
    
    let arr2 = [{
      name: "John",
      job: "engineer",
      address: "abc",
      from: "boston",
      gender: "male"
    },{
      name: "Steph",
      job: "worker",
      address: "uhuh",
      from: "uk",
      gender: "male"
    },{
      name: "dor",
      job: "farmer",
      address: "gdgs",
      from: "us",
      gender: "female"
    }
    ];

Needed Output:

[{
  name: "John",
  job: "engineer",
  from: "boston"
},{
  name: "Steph",
  job: "worker",
  from: "uk"
},{
  name: "Ram",
  job: "farmer",
  from: "us"
}
];

I tried doing this but I am getting only the last values in arr2.

for(let index = 0; index < arr2.length; index++){
  for(let newi = 0; newi < arr1.length; newi++){
    newObj = arr1[newi].field;
    final[newObj] = arr2[index][newObj]
    last.push(final[newObj])
   
  }
  console.log(last)
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Do it like this, using Array.map & Array.reduce

const arr1 = [{  field: "name",  value: "some1",  value1: "some2"},{  field: "job",  value: "some1",  value1: "some2"},{  field: "from",  value: "some1",  value1: "some3"}];
const arr2 = [{  name: "John",  job: "engineer",  address: "abc",  from: "boston",  gender: "male"},{  name: "Steph",  job: "worker",  address: "uhuh",  from: "uk",  gender: "male"},{  name: "dor",  job: "farmer",  address: "gdgs",  from: "us",  gender: "female"}];

const result = arr2.map(item => arr1.reduce((acc, {field}) => {
  acc[field] = item[field];
  return acc;
}, {}));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Using map and Object.assign

const arr1 = [{  field: "name",  value: "some1",  value1: "some2"},{  field: "job",  value: "some1",  value1: "some2"},{  field: "from",  value: "some1",  value1: "some3"}];
const arr2 = [{  name: "John",  job: "engineer",  address: "abc",  from: "boston",  gender: "male"},{  name: "Steph",  job: "worker",  address: "uhuh",  from: "uk",  gender: "male"},{  name: "dor",  job: "farmer",  address: "gdgs",  from: "us",  gender: "female"}];


const fields = arr1.map(({ field }) => field);
const output = arr2.map((item) =>
  Object.assign({}, ...fields.map((field) => ({ [field]: item[field] })))
);


console.log(output)

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