Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda