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

146
Views
Valor de campo de la primera matriz para obtener los valores de la segunda matriz en Javascript

Tengo dos matrices, arr1 tiene el campo y necesito tomar solo el valor del campo. El valor del campo debe verificarse con arr2 y, si coincide con el nombre de la clave, debe crearse como el resultado a continuación:

 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" } ];

Salida necesaria:

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

Intenté hacer esto, pero solo obtengo los últimos valores en 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 answers
Answer question

0

Hazlo así, usando Array.map y 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 Report

0

Usando map y 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 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!