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

154
Views
compare 2 matrices basadas en id como clave y cree 2 matrices más a partir de la comparación

Tengo 2 matrices (array1 y array2). Quiero comparar array1 y array2 usando 'id' como clave y quiero crear 2 matrices resultantes de array2.

  1. La primera matriz debe contener todos los objetos que están presentes en la matriz2 pero no en la matriz1.
  2. La segunda matriz debe contener todos los objetos que están presentes en array1 y array2, ambos comunes.
 const array1 = [{ id: 176770 }, { id: 176771 }, { id: 176820 }]; const array2 = [ { id: 176770, classfication: "comeone", type: "typeee" }, { id: 176771, classfication: "comeone1", type: "typeee1" }, { id: 176820, classfication: "comeone2", type: "typeee2" }, { id: 176670, classfication: "comeone", type: "typeee" }, { id: 176761, classfication: "comeone1", type: "typeee1" }, { id: 176845, classfication: "comeone2", type: "typeee2" }, ];

Por favor, ayúdame a crear estas 2 matrices.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede recorrer la segunda matriz y find un elemento con la misma id en la primera matriz. Si no hay ningún elemento presente en la primera matriz, entonces se cumple su primera condición (guárdelo en result1 ). Si el elemento existe en la primera matriz, su segunda condición se cumple (Almacenarlo en result2 )

 const array1 = [{ "id": 176770 }, { "id": 176771 }, { "id": 176820 }] const array2 = [{ "id": 176770, "classfication": "comeone", "type": "typeee" }, { "id": 176771, "classfication": "comeone1", "type": "typeee1" }, { "id": 176820, "classfication": "comeone2", "type": "typeee2" }, { "id": 176670, "classfication": "comeone", "type": "typeee" }, { "id": 176761, "classfication": "comeone1", "type": "typeee1" }, { "id": 176845, "classfication": "comeone2", "type": "typeee2" }] let result1 = [] let result2 = []; for (let element of array2) { let existingInFirst = array1.find(ele => ele.id == element.id); if (!existingInFirst) { result1.push(element) } else{ result2.push(element) } } console.log(result1) console.log(result2)

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!