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

174
Views
matriz de filtro basada en otras matrices dinámicamente

Soy nuevo en JS. Quiero aplicar un filtro en la matriz en función de otras matrices. Por ejemplo: filtre la matriz de data en función de los nombres de propiedad de colNames y los valores de la matriz de Values . Las matrices colNames y Values pueden tener cualquier longitud (no siempre 2).

 var data = [{ "name": "Tiger Nixon", "position": "System Architect", "salary": "320800", "start_date": "2011\/04\/25", "office": "Edinburgh", "rating": "5421" }, { "name": "Garrett Winters", "position": "Accountant", "salary": "170750", "start_date": "2011\/07\/25", "office": "Tokyo", "rating": "8422" }, { "name": "Garrett Winters", "position": "Analyst", "salary": "170750", "start_date": "2011\/07\/25", "office": "Tokyo", "rating": "8422" }, { "name": "Ashton Cox", "position": "Junior Technical Author", "salary": "86000", "start_date": "2009\/01\/12", "office": "San Francisco", "rating": "1562" }, { "name": "Cedric Kelly", "position": "Senior Javascript Developer", "salary": "433060", "start_date": "2012\/03\/29", "office": "Edinburgh", "rating": "6224" } ] var colNames = ['name', 'position'] var Values = ['Garrett Winters', 'Accountant']

Gastos esperados

 [{ "name": "Garrett Winters", "position": "Accountant", "salary": "170750", "start_date": "2011\/07\/25", "office": "Tokyo", "rating": "8422" }]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede filtrar con la iteración de todas las claves y verificar con los valores.

 const data = [{ name: "Tiger Nixon", position: "System Architect", salary: "320800", start_date: "2011\\/04\\/25", office: "Edinburgh", rating: "5421" }, { name: "Garrett Winters", position: "Accountant", salary: "170750", start_date: "2011\\/07\\/25", office: "Tokyo", rating: "8422" }, { name: "Garrett Winters", position: "Analyst", salary: "170750", start_date: "2011\\/07\\/25", office: "Tokyo", rating: "8422" }, { name: "Ashton Cox", position: "Junior Technical Author", salary: "86000", start_date: "2009\\/01\\/12", office: "San Francisco", rating: "1562" }, { name: "Cedric Kelly", position: "Senior Javascript Developer", salary: "433060", start_date: "2012\\/03\\/29", office: "Edinburgh", rating: "6224" }], cols = ['name', 'position'], values = ['Garrett Winters', 'Accountant'], result = data.filter(o => cols.every((k, i) => o[k] === values[i])); console.log(result);
 .as-console-wrapper { max-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!