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

166
Views
Go to next or previous item if a key does not exist in an array

What I'm trying to achieve looks quite tricky to me. I want to move from one item to another which I am able to easily achieve with this code snippet as shown below.

Assuming I am having this array of Objects

const arr = [
{ answered: true, duration: 15 },  
{ answered: true, duration: 15 },
{ duration: 15 },
{ answered: true, duration: 15 },
{ duration: 15 },
];

Then I use this code snippet to achieve moving to the next item which works fine.

let count = 0;
arr[count + 1];
return arr

However, What I am trying to achieve is move to the next item that doesn't have a answered: true key:value Meaning if I'm starting from the 0 index the next button should move to 3rd index because it doesn't have a answered: true

For more clarity, If I am able to get items that does not contain answered filtered out, I have to be able to do a count + 1 that can also move me to the next array index in the filtered result.

Assuming the filtered result is

const filtered = [
{ duration: 15 },
{ duration: 15 },
]

I should be able to do filtered[count + 1]

Thank you

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

0

Assuming that both the iterator and the array are part of a global state, the function click() aims to simulate a button click that you mentioned.

The function loops through the array and starts searching for the next unanswered element, starting from the global iter value. The function sets the iter to be one after the current element that is not answered, so that when the function is called again, it starts searching from that next element.

The reset() function simply restarts the search again from the start.

let iter = 0

const arr = [{
    answered: true,
    duration: 15
  },
  {
    answered: true,
    duration: 15
  },
  {
    duration: 10
  },
  {
    answered: true,
    duration: 15
  },
  {
    answered: true,
    duration: 15
  },
  {
    answered: true,
    duration: 15
  },
  {
    answered: true,
    duration: 15
  },
  {
    duration: 3
  },
  {
    duration: 1
  },
];


const findNext = () => {
  for (let i = iter; i < arr.length; i++) {
    if (!('answered' in arr[i]) || !arr[i].answered) {
      document.getElementById("resultIndex").innerHTML = `Result id: ${i}`;
      document.getElementById("result").innerHTML = `Result body: ${JSON.stringify(arr[i])}`;
      iter = i + 1
      return
    }
  }
}

const reset = () => {
  iter = 0
  document.getElementById("resultIndex").innerHTML = `Result id: none`;
  document.getElementById("result").innerHTML = `Result body: none`;
}
<div>
  <button onClick={findNext()}>Next</button>
  <button onClick={reset()}>Reset</button>
</div>
<div>
  <div id='resultIndex'>Result id: none</div>
  <div id='result'>Result body: none</div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can filter the array with elements that do not contain answered, then loop on them in the normal way.

var index = 0;

const arr = [
{ answered: true, duration: 15 },  
{ answered: true, duration: 15 },
{ duration: 15 },
{ answered: true, duration: 15 },
{ duration: 15 },
{ answered: true, duration: 15 },  
{ answered: true, duration: 15 },
{ duration: 15 },
{ answered: true, duration: 15 },
{ duration: 15 },
];

function findNext() {
  for (item in arr) {
    if (!arr[item].hasOwnProperty('answered')) {
      document.getElementById("index").setAttribute('value',item);
      break;
    }
  }
}
<button onClick="findNext()">Find next</button>

<input type="text" value="" id="index">

about 4 years ago · Juan Pablo Isaza Report

0

You should try something like this :

const arr = [
{ answered: true, duration: 15 },
{ duration: 15 },
{ answered: true, duration: 15 },
{ duration: 15 }
]
var i = 0
var missing

for (const element of arr) {
  if (!element.answered) { 
    missing = element
    break
  }
  i++
}

console.log(missing)
console.log(i)
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!