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

90
Vistas
Using a variable as a key name inside a filter function

Trying to figure out how to use a variable as a key name inside a filter function.

Example array of objects:

const data = [{"itemID": 11, "Complete": true, "Started": true},{"itemID": 16, "Complete": false, "Started": false}];

Conditional statement:

if(isStartMode){
    const currentMode = 'Started'
  } else {
    const currentMode = 'Complete'
  }

Variable

var possible_ItemID = 16;

Filter function

const lineItem = data.filter((item) => item.itemID === possible_ItemID && item.{currentMode} !== true)

console.log(lineItem)

I'm looking for a way to switch either "Started" or "Complete" where it says {currentMode}, without having to make separate hardcoded statements for each: item.Started or item.Complete

There's just something I'm missing in the syntax...

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

0

Okay, I should have just done it like this - use [], and declare the variable outside of the if statement.

var currentMode;
if(isStartMode){
    currentMode = 'Started'
  } else {
    currentMode = 'Complete'
  }

const lineItem = data.filter((item) => item.itemID === possible_ItemID && item[currentMode] !== true)
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