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

130
Views
obtener la matriz de objetos en función de la propiedad misma usando javascript

Tengo dos arreglos de objetos, arr1 y arr2

si taskId e id son iguales, recupérelos de arr1

Cómo verificar según la propiedad de arr1 y arr2 en javascript

 var arr1= [ {"name":"refresh task","memberIds":[981],"dueOn":"2022-08-30","taskId":19}, {"name":"ref one","memberIds":[981,982],"dueOn":"2022-08-25","taskId":null} ] var arr2 =[ { "country": "IN", "tasks": [ {id: 19, "name": "abc" }, {id: 20, "name": "xyz" } ] } ] I tried var result = arr1.filter(e=>e.taskId===(arr2.map(i=>i.tasks.map(t=>t.id)) Expected Output [ {"name":"refresh task","memberIds":[981],"dueOn":"2022-08-30","taskId":19} ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  • Con Set , Array#flatMap y [ Array#map ][3], get the list of task ids in arr2``
  • Usando Array#filter y Set#has , devuelva la matriz resultante donde taskId está en el conjunto anterior

 const arr1= [ {"name":"refresh task", "memberIds":[981], "dueOn":"2022-08-30", "taskId":19}, {"name":"ref one", "memberIds":[981,982], "dueOn":"2022-08-25", "taskId":null} ], arr2 =[ { "country": "IN", "tasks": [ {"id": 19, "name": "abc"}, {"id": 20, "name": "xyz"} ] } ]; const taskIdSet = new Set( arr2.flatMap(({ tasks = [] }) => tasks.map(({ id }) => id)) ); const result = arr1.filter(({ taskId }) => taskIdSet.has(taskId)); console.log(result);

EDITAR: para obtener "Tengo dos matrices de objetos, arr1 y arr2 si taskId e id no son iguales, luego recupere de arr2":

 const arr1= [ {"name":"refresh task", "memberIds":[981], "dueOn":"2022-08-30", "taskId":19}, {"name":"ref one", "memberIds":[981,982], "dueOn":"2022-08-25", "taskId":null} ], arr2 =[ { "country": "IN", "tasks": [ {"id": 19, "name": "abc"}, {"id": 20, "name": "xyz"} ] } ]; const taskIdSet = arr1.reduce((taskIdSet, { taskId }) => { if(taskId !== null) { taskIdSet.add(taskId); } return taskIdSet; }, new Set); const result = arr2 .flatMap(({ tasks = [] }) => tasks) .filter(({ id }) => !taskIdSet.has(id)); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Esto también funciona:

 var result = arr1.filter(e=>{ for(let obj of arr2){ for(let task of obj.tasks){ return e.taskId === task.id; } } })
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!