简体   繁体   中英

How to keep checking array until certain condition is met

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.

  • 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));

Ok, I think I kind of understand the logic, but I'm not sure. Is the question: I want to check if any of the ids following the id corresponding with my value, is not -1 ?

Hope I understood the logic correctly.

If you have no use for reusable functions, or don't care about structure, you can write this very short as well:

 var pos = 0; var idarray = [ -1, 2, -1, 4, -1 ]; var array = [ 3, 2, 1, 0, 7 ]; var get_result = ( array, idarray, pos, ex ) => { const offset = array.indexOf( pos ) + 1; return idarray .slice( offset ) .reduce(( result, id, index ) => { if ( result === "final index" && id !== -1 ) result = array[ index + offset ]; return result; }, "final index" ); }; // example 1: const ex1_search_value = 0; // pos const ex1_ids = [ -1, 2, -1, 4, -1 ]; // idarray const ex1_values = [3, 2, 1, 0, 7]; // array // expect "final index", since our range will only contain the last id, which is -1 const result1 = get_result( ex1_values, ex1_ids, ex1_search_value ); console.log( `expect final index, ${ result1 }` ); // example2: const ex2_search_value = 2; const ex2_ids = [ -1, 2, -1, -1, -1 ]; const ex2_values = [3, 2, 1, 0, 7]; // expect "final index", since our range is the last two items, both with id -1 const result2 = get_result( ex2_values, ex2_ids, ex2_search_value ); console.log( `expect final index, ${ result2 }` ); // example3: const ex3_search_value = 2; const ex3_ids = [ -1, 2, -1, -1, -1, -1, -1, -1, -1, 3, -1, 2, -1, -1 ]; const ex3_values = [ 3, 2, 1, 0, 7, 4, 9, 14, 74, 8, 45, 14, 17, 84 ]; // expect { id: 3, value: 8 } const result3 = get_result( ex3_values, ex3_ids, ex3_search_value ); console.log( `expect 8, ${ result3 }` ); // example4: const ex4_search_value = 2; const ex4_ids = [-1, 2, -1, 4, 1]; const ex4_values = [ 3, 2, 1, 0, 7]; // expect { id: 4, value: 0 } const result4 = get_result( ex4_values, ex4_ids, ex4_search_value ); console.log( `expect 0, ${ result4 }` ); // example5: const ex5_search_value = 2; const ex5_ids = [-1, 2, -1, -1, 1]; const ex5_values = [ 3, 2, 1, 0, 7]; // expect { id: 1, value: 7 } const result5 = get_result( ex5_values, ex5_ids, ex5_search_value ); console.log( `expect 7, ${ result5 }` ); // example6: const ex6_search_value = 2; const ex6_ids = [-1, 2, -1, -1, -1]; const ex6_values = [ 3, 2, 1, 0, 7]; // expect "final index" const result6 = get_result( ex6_values, ex6_ids, ex6_search_value ); console.log( `expect final index, ${ result6 }` );

My other approach here is to merge the arrays into one array containing objects, so that we do not have to check for undefined values, while still being able to use array methods instead of plain loops. This would help if you have to use the id/value combinations alot in the code past this point. The functions are just there to make everything reusable.

 // Create an object from the id and value combinations. const create_collection = ( ids, values ) => { return ids.map(( id, index ) => ({ id, value: values[ index ] })); }; const has_valid_descendants = ( collection, search_value ) => { // Find the index of the first item that has our requested value. const search_index = collection.findIndex( item => item.value === search_value ); // Slice the relevant part from the collection. // Since we will only look at records past the item ahving the search_value, we mights well only slice the relevant parts. const collection_in_range = collection.slice( search_index + 1 ); // Find the first item in range has an id that is not -1. return collection_in_range.find( item => item.id !== -1 ) || 'final index'; }; // example 1: const ex1_search_value = 0; // pos const ex1_ids = [ -1, 2, -1, 4, -1 ]; // idarray const ex1_values = [3, 2, 1, 0, 7]; // array // Collection should be: [{ id: -1, value: 3 },{ id: 2, value: 2 },{ id: -1, value: 1 },{ id: 4, value: 0 },{ id: -1, value: 7 }]; const ex1_collection = create_collection( ex1_ids, ex1_values ); console.log( ex1_collection ); // Is there a valid next item? // expect "final index", since our range will only contain the last id, which is -1 const ex1_result = has_valid_descendants( ex1_collection, ex1_search_value ); console.log( 'expect 1: "final index"' ); console.log( `example 1: ${ JSON.stringify( ex1_result ) }` ); // example2: const ex2_search_value = 2; const ex2_ids = [ -1, 2, -1, -1, -1 ]; const ex2_values = [3, 2, 1, 0, 7]; // expect "final index", since our range is the last two items, both with id -1 const ex2_result = has_valid_descendants( create_collection( ex2_ids, ex2_values ), ex2_search_value ); console.log( 'expect 2: "final index"' ); console.log( `example 2: ${ JSON.stringify( ex2_result ) }` ); // example3: // We add a bunch of other values and ids. // This proves it will work with longer arrays as well // and that the result is the first item without the id -1 const ex3_search_value = 2; const ex3_ids = [ -1, 2, -1, -1, -1, -1, -1, -1, -1, 3, -1, 2, -1, -1 ]; const ex3_values = [ 3, 2, 1, 0, 7, 4, 9, 14, 74, 8, 45, 14, 17, 84 ]; // expect { id: 3, value: 8 } const ex3_result = has_valid_descendants( create_collection( ex3_ids, ex3_values ), ex3_search_value ); console.log( 'expect 3: { id: 3, value: 8 }"' ); console.log( `example 3: ${ JSON.stringify( ex3_result ) }` ); // example4: // Note: I've added || 'final index'; to the has_valid_descendants() function. const ex4_search_value = 2; const ex4_ids = [-1, 2, -1, 4, 1]; const ex4_values = [3, 2, 1, 0, 7]; // expect { id: 4, value: 0 } const ex4_result = has_valid_descendants( create_collection( ex4_ids, ex4_values ), ex4_search_value ); console.log( 'expect 4: { id: 4, value: 0 }' ); console.log( `example 4: ${ JSON.stringify( ex4_result ) }` ); // example5: // Note: I've added || 'final index'; to the has_valid_descendants() function. const ex5_search_value = 2; const ex5_ids = [-1, 2, -1, -1, 1]; const ex5_values = [3, 2, 1, 0, 7]; // expect { id: 1, value: 7 } const ex5_result = has_valid_descendants( create_collection( ex5_ids, ex5_values ), ex5_search_value ); console.log( 'expect 5: { id: 1, value: 7 }' ); console.log( `example 5: ${ JSON.stringify( ex5_result ) }` ); // example6: // Note: I've added || 'final index'; to the has_valid_descendants() function. const ex6_search_value = 2; const ex6_ids = [-1, 2, -1, -1, -1]; const ex6_values = [3, 2, 1, 0, 7]; // expect "final index" const ex6_result = has_valid_descendants( create_collection( ex6_ids, ex6_values ), ex6_search_value ); console.log( 'expect 6: "final index"' ); console.log( `example 6: ${ JSON.stringify( ex6_result ) }` );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM