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

220
Vistas
Dynamically change the key in a filter query

I've created a simple filtering code that filters out 0s and nulls. Perhaps not the most elegant, but it works:

var data = [{"location": 30, "year": 2019},
            {"location": 60, "year" : 1928},
            {"location": 0, "year": 2019},
            {"location": 50, "year": 0}];
    
var filteredData = [];

for (var i = 0; i < data.length; ++i) {
  if (data[i].location !== null && data[i].location != 0) {
    filteredData.push({
      location: data[i].location,
      year: data[i].year
    })
  }
};
console.log(filteredData);

Now I would like to filter out 0s and nulls for other keys (e.g. year), how can I dynamically change key?

My (perhaps naive) thinking was to create a variable indicating the key I want to filter out

Unfortunately that didn't work. Does anyone have a suggestion or help me in the right direction?

var var1 = 'year';
    
var data = [{"location": 30, "year": 2019},
            {"location": 60, "year" : 1928},
            {"location": 0, "year": 2019},
            {"location": 50, "year": 0}];

var filteredData = [];

for (var i = 0; i < data.length; ++i) {
  if (data[i].var1 !== null && data[i].var1 != 0) {
    filteredData.push({
      location: data[i].location,
      year: data[i].year
    })
  }
};

console.log(filteredData)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your issue is that you need to use bracket notation instead of dot notation for variable keys.

That said, JavaScript has an Array filter method

You can then use every to have dynamic keys in an array:

const data = [{"location": 30, "year": 2019},
            {"location": 60, "year" : 1928},
            {"location": 0, "year": 2019},
            {"location": 50, "year": 0}];

const notZero = ["location","year"]

const filteredData = data.filter(item => notZero.every(key => item[key] !== 0))

console.log(filteredData)

about 4 years ago · Juan Pablo Isaza Denunciar

0

instead of data[i].var1 try data[i][var1]. the square brackets allows you to have a dynamic key.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you can have 0 or null in your data, you need to do this:

const data = [{"location": 30, "year": 2019},
            {"location": 60, "year" : 1928},
            {"location": 0, "year": 2019},
            {"location": 50, "year": 0}];

const filteredData = data.filter(item => !Object.values(item).filter(value => !value).length)

This lets you dynamically check all values within an object and check if all of them are truthy values, thereby catching all falsy values.

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