0

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...

Ben in CA
  • 688
  • 8
  • 22

1 Answers1

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)
Ben in CA
  • 688
  • 8
  • 22