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

200
Views
Nodejs - How to find the index with two conditions, one condition being where a property exists

I have an object that looks like this:

{
   "examples":[
      {
         "key":"value1"
      },
      {
         "key":"value1",
         "key2":"example"
      },
      {
         "key":"value1"
      },
      {
         "key":"value2"
      },
      {
         "key":"value2",
         "key2":"example"
      },
      {
         "key":"value2"
      }
   ]
}

I'm trying to use findIndex to find where in the object that key === 'value1' and key2 exists (so in this instance the index would be 1). I've tried using something like var x = examples.findIndex(({key}) => key === 'value1' && ({key2}) => key2) but it's not working. How do I go about this? Any answers would be greatly appreciated! :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

examples is a property of the data object, so you need to iterate over that array. examples.findIndex won't do anything.

You can then check to see if key has "value1" as a value, and that there is a key2 in the object.

const data={examples:[{key:"value1"},{key:"value1",key2:"example"},{key:"value1"},{key:"value2"},{key:"value2",key2:"example"},{key:"value2"}]};

const result = data.examples.findIndex(obj => {
  return obj.key === 'value1' && obj.key2;
});

console.log(result);

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!