Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
filter array based on another arrays dynamically

I am new to JS. I want to apply filter on array based on other arrays. For example : Filter data array based on property names from colNames and values from Values array. The colNames and Values arrays can have any length (not always 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']

Expected Outcome

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

0

You could filter with the iteration of all keys and check with the values.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda