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

169
Views
Cómo devolver una matriz de objetos al verificar su propiedad y lista de matrices en javascript

cómo comparar una matriz de objetos con matrices usando javascript

Necesito verificar la matriz de objetos según las condiciones.

  1. si el targetvalue es el mismo y code no tiene valor en la lista de arraylist devuelve una matriz de objetos

  2. de lo contrario, si el targetvalue es el mismo y code mismo valor en la lista de arraylist devuelve una matriz de objetos

si no volver []

 var arraylist =["IT","FI"]; var arrobj1 =[ {id:1, name: "sonu", code: "IT", targetvalue: "908"}, {id:2, name: "abi", code: "IT", targetvalue: "834"}, {id:3, name: "lisa", code: "SP", targetvalue: "834"}, {id:4, name: "ben", code: "FI", targetvalue: "234"}, ] Expected Output //same value , and has "IT" [] **** var arrobj2 =[ {id:1, name: "sonu", code: "IT", targetvalue: "908"}, {id:2, name: "abi", code: "IT", targetvalue: "834"}, {id:3, name: "lisa", code: "SG", targetvalue: "234"}, {id:4, name: "ben", code: "SP", targetvalue: "234"}, ] Expected Output //same targetvalue and no include of `FI or IT` so return [ {id:3, name: "lisa", code: "SG", targetvalue: "234"}, {id:4, name: "ben", code: "SP", targetvalue: "234"}, ] **** var arrobj3 =[ {id:1, name: "sonu", code: "IT", targetvalue: "908"}, {id:3, name: "lisa", code: "FI", targetvalue: "234"}, {id:4, name: "ben", code: "FI", targetvalue: "234"}, ] Expected Output // targetvalue and code same [ {id:3, name: "lisa", code: "FI", targetvalue: "234"}, {id:4, name: "ben", code: "FI", targetvalue: "234"}] I tried const checkIdList = list => { const idlist = ['IN', 'FI']; const resultarray = list .map((obj, i) => list.find((elem, index) => { if (i !== index && elem.targetvalue === obj.targetvalue && (element.code === obj.code || idlist.includes(element.code)) { return obj; } })) .filter(x => x); return resultarray; }; var finalResult = this.checkIdList(arrobj1);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede comenzar agrupando los elementos por targetValue y luego filtrar los Object.values del resultado por sus tres condiciones:

 function checkIdList(array, list) { // group by targetValue const grouped = {}; for (const o of array) { (grouped[o.targetvalue] ??= []).push(o); } return Object.values(grouped) .filter(g => g.length > 1 // subarrays of length > 1 indicate duplicates && ( g.every(({ code }) => !list.includes(code)) || // if every code is not in the list g.every(({ code }, _, a) => code === a[0].code) // OR include if all codes are equal and included in list )) .flat(); } const arraylist = ["IT", "FI"]; const arrobj1 = [{ id: 1, name: "sonu", code: "IT", targetvalue: "908" }, { id: 2, name: "abi", code: "IT", targetvalue: "834" }, { id: 3, name: "lisa", code: "SP", targetvalue: "834" }, { id: 4, name: "ben", code: "FI", targetvalue: "234" },]; const arrobj2 = [{ id: 1, name: "sonu", code: "IT", targetvalue: "908" }, { id: 2, name: "abi", code: "IT", targetvalue: "834" }, { id: 3, name: "lisa", code: "SG", targetvalue: "234" }, { id: 4, name: "ben", code: "SP", targetvalue: "234" },]; const arrobj3 = [{ id: 1, name: "sonu", code: "IT", targetvalue: "908" }, { id: 3, name: "lisa", code: "FI", targetvalue: "234" }, { id: 4, name: "ben", code: "FI", targetvalue: "234" },]; console.log(checkIdList(arrobj1, arraylist)); console.log(checkIdList(arrobj2, arraylist)); console.log(checkIdList(arrobj3, arraylist));

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!