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

232
Views
encuentre la PRIMERA aparición del elemento de Array1 EN Array2, tome el índice en el que se encuentra el elemento en Array2 y cree una nueva matriz

Tengo dos matrices. mire por id, encuentre la PRIMERA aparición del elemento de Array1 EN Array2, tome el índice en el que se encuentra el elemento en Array2 y cree una nueva matriz donde el elemento en Array1 se mueve al nuevo índice que se encuentra en Array2. en Array1 {id: 001} está en el índice 0, me gustaría que estuviera en un nuevo Array en el índice 1 (el índice se encuentra PRIMERO en Array2). {id: 002} está en el índice 1 en Array1, me gustaría que esté en el nuevo Array en el índice 3 (el índice se encuentra PRIMERO en Array2), etc.

 const Array1 = [ { id: '001', school: "blue springs" }, { id: '002', school: "sycamore hills" }, { id: '003', school: "moreland ridge" }, { id: '004', school: "grain valley" } ] const Array2 = [ { id: '003', participant: "Susan" }, { id: '001', participant: "Henry" }, { id: '003', participant: "Justin" <---- if 003 exists do not duplicate the return array, this is my issue.... }, { id: '004', participant: "Jessica" }, { id: '002', participant: "Carly" }, { id: '001', participant: "Chloe" <---- if 001 exists do not duplicate the return array, this is my issue.... } // got this far, const finder = Array1.map((el) => { return Array2.findIndex((el2) => { return el2.id === el.id}) }) console.log(finder) // [ 1, 4, 0, 3 ] <--- need to move Array1 objects to these new indexes

Rendimiento esperado

 const Result = [ { id: '003', school: "moreland ridge" }, { id: '001', school: "blue springs" }, { id: '004', school: "grain valley" }, { id: '002', school: "sycamore hills" } ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Primero filtra para eliminar duplicados en Array2 y luego busca una coincidencia en Array1 con respecto a las identificaciones

 const Array1 = [ { id: '001', school: "blue springs" }, { id: '002', school: "sycamore hills" }, { id: '003', school: "moreland ridge" }, { id: '004', school: "grain valley" } ] const Array2 = [ { id: '003', participant: "Susan" }, { id: '001', participant: "Henry" }, { id: '003', participant: "Justin" // <---- if 003 exists do not duplicate the return array, this is my issue.... }, { id: '004', participant: "Jessica" }, { id: '002', participant: "Carly" }, { id: '001', participant: "Chloe" // <---- if 001 exists do not duplicate the return array, this is my issue.... } ] const alreadyShown = {}; const res = Array2.filter( el => { if (!alreadyShown[el.id]){ alreadyShown[el.id] = true; return true; } return false; }).map( x => Array1.find( y => x.id === y.id ) || x); console.log(res)

NOTA: Incluí un OR lógico para proporcionar un caso alternativo, pero esto no está definido en la solicitud OP

about 4 years ago · Juan Pablo Isaza Report

0

Creo que solo necesita ordenar la primera matriz por la singularidad del identificador en la segunda matriz

 const Array1 = [{id: '001',school: "blue springs"},{id: '002',school: "sycamore hills"},{id: '003',school: "moreland ridge"},{id: '004',school: "grain valley"}]; const Array2 = [{id: '003',participant: "Susan"},{id: '001',participant: "Henry"},{id: '003',participant: "Justin"},{id: '004',participant: "Jessica"},{id: '002',participant: "Carly"},{id: '001',participant: "Chloe" }]; const idOrder = Array2.map(({ id }) => id).filter((v, i, a) => a.indexOf(v)===i); console.log('Order:', idOrder ); const sortedByOrder = Array1.sort(({ id: id1 }, { id: id2 }) => idOrder.indexOf(id1) - idOrder.indexOf(id2)); console.log('Result:', sortedByOrder);
 .as-console-wrapper{min-height: 100%!important; top: 0}

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!