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

167
Views
Filtrado en una matriz anidada de objetos para devolver el objeto coincidente completo

Mi matriz de objetos anidados se ve así junto con la matriz para que coincida con

 let findThis = ["Water"]; let arrayOfElements = [ { "code": "a", "templates": [ { "templateCode": "Water", "title": "Earth" }, { "templateCode": "Milk", "title": "Sky" } ] }, { "code": "b", "templates": [] }, { "code": "c", "templates": [ { "templateCode": "Water", "title": "Earth" }, { "templateCode": "Tree", "title": "Moon" } }, { "code": "d", "templates": [ { "templateCode": "Tooth", "title": "Tiger" } } ]

Quiero extraer aquellos objetos cuyo templateCode sea Water . Así que mi última matriz devuelta debería verse así.

 let result = [ { "code": "a", "templates": [ { "templateCode": "Water", "title": "Earth" }, { "templateCode": "Milk", "title": "Sky" } ] }, { "code": "c", "templates": [ { "templateCode": "Water", "title": "Earth" }, { "templateCode": "Tree", "title": "Moon" } } ]

Lo intenté:

 arrayOfElements.filter(x => x.templates.filter(y => findThis.includes(y.templateCode)));

Pero está devolviendo la matriz completa y no funciona.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Debe filter arrayOfElements con la condición de cualquier templateCode existente dentro de findThis array.

 let findThis = ["Water"]; let arrayOfElements = [ { "code": "a", "templates": [ { "templateCode": "Water", "title": "Earth" }, { "templateCode": "Milk", "title": "Sky" } ] }, { "code": "b", "templates": [] }, { "code": "c", "templates": [ { "templateCode": "Water", "title": "Earth" }, { "templateCode": "Tree", "title": "Moon" } ] }, { "code": "d", "templates": [ { "templateCode": "Tooth", "title": "Tiger" } ] } ] const result = arrayOfElements.filter(item => item.templates.some(t => findThis.includes(t.templateCode))) console.log(result)

about 4 years ago · Santiago Gelvez 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!