How to keep checking array until certain condition is met

Shinjo

I have two arrays idarray and array and want to find item_tosearch in idarray. Then using the found index loop forward through idarray until an element is found that is not -1. Then use that index to retreive the value from array.

From what I know, if you want to keep checking you can use any sort of iteration either for or while or foreach in this case, I've got 2 arrays. First is for idarray and second is for array. 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 id wasn't -1.

What I've tried:

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 > -1) {
  var res = array.slice(index);
}

if (res != undefined) {
  for (let i = 0; i < 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');
}

My question is, is there any way I could've improved the method?

Any advice is apreciated.


Clarification:

If I have the following:

idarray = [-1, 2, -1, 4, 1]; array = [3, 2, 1, 0, 7];

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.


Another case, if I had:

idarray = [-1, 2, -1, -1, 1]; array = [3, 2, 1, 0, 7];

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.

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.

I've tried another iteration to fetch but doesn't seem to get what I want:

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 > -1) {
  var res = array.slice(index);
}

if (res != undefined) {
  for (let i = 0; i < res.length; i++) {
    if (res[i + 1] != undefined) {
      if (idarray[index + 1] == -1) {
        for (let j = i + 1; j < 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');
}

3limin4t0r

If I understand your question good enough you're looking for something like this. Even if this isn't exactly the solution you want you might be able to get some inspiration from it.

  • This solution starts by finding the index of the element to search in idarray. If it can't be found return undefined.
  • Next start looping from 1 index higher until the end of the idarray. If an element is found that is not -1 return the element on the current index from array.
  • If nothing is found undefined is returned.

var idarray, array;

function giveThisABetterName(item_tosearch, idarray, array) {
  var index = idarray.indexOf(item_tosearch);
  if (index === -1) return; // data not found

  for (index += 1; index < idarray.length; ++index) {
    if (idarray[index] !== -1) return array[index];
  }
  
  // reach end of array
}

idarray = [-1, 2, -1, 4, 1];
array   = [ 3, 2,  1, 0, 7];
console.log(giveThisABetterName(2, idarray, array));

idarray = [-1, 2, -1, -1, 1];
array   = [ 3, 2,  1,  0, 7];
console.log(giveThisABetterName(2, idarray, array));

idarray = [-1, 2, -1, -1, 1];
array   = [ 3, 2,  1,  0, 7];
console.log(giveThisABetterName(9, idarray, array));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to keep a value constant until a certain condition is met

How to stop the program until a certain condition is met?

How do I roll a list or numpy array until a certain condition is met?

How to block checkboxes for checking when certain condition is met in jQuery?

How to schedule a macro, and to keep trying it until a condition is met?

Keep calling a function until a condition is met Python

python: continue iteration until a certain condition is met

NodeJS - While loop until certain condition is met or certain time is passed

Javascript - checking whether a condition met in Array forEach

how to keep data from lines in file until condition met later in file python

How do I keep passing the output of a Halide pipeline back into the pipeline until some condition is met?

How to implement a call repetition until a certain condition is met using project reactor?

How do I continuously show an AlertDialog in a do while loop until a certain condition is met?

R - Conditional lagging - How to lag a certain amount of cells until a condition is met?

How to slice a pandas MultiIndex df keeping all values until a certain condition is met?

Trying to make function keep running until a condition is met

use RXJS to keep triggering http calls in angular until a condition is met

How to retry until some condition is met

How to await a coroutine until a condition is met?

How to sum in each cell until a condition is met

Is there a J idiom for adding to a list until a certain condition is met?

Looping in a data frame in R until a certain condition is met

delay execution of a jQuery/javascript function until certain condition is met

I need help in an if else statement in R until a certain condition is met

Counting distinct, until a certain condition based on another row is met

Construct a variable that conditionally takes a certain value until another condition is met

How can I only keep items of an array that match a certain condition?

Keep certain elements in a list of tuples where condition is met

How to end a function if certain condition is met in pandas