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');
}
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.
idarray
. If it can't be found return undefined
.idarray
. If an element is found that is not -1
return the element on the current index from array
.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.
Comments