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

87
Views
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 answers
Answer question

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