title: How to keep checking array until certain condition is met
body: <p>I have two arrays <code>idarray</code> and <code>array</code> and want to find <code>item_tosearch</code> in <code>idarray</code>. Then using the found index loop forward through <code>idarray</code> until an element is found that is not <code>-1</code>. Then use that index to retreive the value from <code>array</code>.</p>
  
  <p>From what I know, if you want to keep checking you can use any sort of iteration either <code>for</code> or <code>while</code> or <code>foreach</code> in this case, I've got 2 arrays. First is for <code>idarray</code> and second is for <code>array</code>. I've managed to check what is the next data and if the data has reached the final value. I also able to get what I want which is the next data as long as the <code>id</code> wasn't <code>-1</code>.</p>
  
  <p>What I've tried:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var item_tosearch = 0;
  var idarray = [-1, 2, -1, 4, -1]
  var array = [3, 2, 1, 0, 7];
  var index = array.indexOf(item_tosearch);
  
  if (index &gt; -1) {
    var res = array.slice(index);
  }
  
  if (res != undefined) {
    for (let i = 0; i &lt; res.length; i++) {
      if (res[i + 1] != undefined) {
        if (idarray[index + 1] == -1) {
          if (res[i + 2] != undefined) {
            console.log("Next = " + res[i + 2]);
            break;
          } else {
            console.log("Final index");
            break;
          }
        } else {
          console.log("Next = " + res[i + 1]);
          break;
        }
      } else {
        console.log("Final index");
      }
    }
  } else {
    console.log('data not found');
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>My question is, is there any way I could've improved the method?</p>
  
  <p>Any advice is apreciated.</p>
  
  <hr>
  
  <p>Clarification:</p>
  
  <p>If I have the following:</p>
  
  <p>idarray = [-1, 2, -1, 4, 1];
  array = [3, 2, 1, 0, 7];</p>
  
  <p>What I would like to have is if I put 2 on item_tosearch as value, I'm expecting to have: 0 as the returned value since it was the next item without -1 in the id. </p>
  
  <hr>
  
  <p>Another case, if I had:</p>
  
  <p>idarray = [-1, 2, -1, -1, 1];
  array = [3, 2, 1, 0, 7];</p>
  
  <p>And if I put 2 on item_tosearch as value, I'm expecting to have: 7 as the returned value since it was the next item without -1 in the id. </p>
  
  <p>But if idarray was = [-1, 2, -1, -1, -1] with the same 2 on item_tosearch as value. I expect "final index" to be returned. Since no more item without -1 as the id.</p>
  
  <p>I've tried another iteration to fetch but doesn't seem to get what I want:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var item_tosearch = 2;
  var idarray = [-1, 2, -1, -1, -1]
  var array = [3, 2, 1, 0, 7];
  var index = array.indexOf(item_tosearch);
  
  if (index &gt; -1) {
    var res = array.slice(index);
  }
  
  if (res != undefined) {
    for (let i = 0; i &lt; res.length; i++) {
      if (res[i + 1] != undefined) {
        if (idarray[index + 1] == -1) {
          for (let j = i + 1; j &lt; res.length - i; j++) {
            if (res[j + 1] != undefined) { // fetch if still got data with id != -1
              console.log("Next = " + res[j + 1]); // should show next item without -1 in id
              break;
            } else {
              console.log("Final index"); // reach end of array
              break;
            }
          }
        } else {
          console.log("Next = " + res[i + 1]); // should show next item without -1 in id
          break;
        }
      } else {
        console.log("Final index"); // reach end of array
      }
    }
  } else {
    console.log('data not found');
  }</code></pre>
  </div>
  </div>
  </p>