I have an array like this:
const array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];
I need to be able to identify any index with "number value" less than 65, and remove that index and the one immediately to the left of it.
So in this case, numbers in the array which are less than 65 are "6" and "57".
I need to remove "6" and "something", as well as "57" and "monkey".
and the resulting array would be:
const array = ["somethingelse", "130", "carrot", "89", "plane", "71"];
I have the following code so far:
const array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];
const range = 65 //I need to specify a range here, and not an exact number
const remove_array_index = array.findIndex(a =>a.includes(range));
if(array > -1){ //having -1 in .splice returns unintended results, so this tests if an index was found that matches the range
array.splice(remove_array_index-1, 2);
}
For the code above I need to specify the number value exactly, which removes that index and the one to the left of it..... The problem is that I need to specify a range, and not an exact value.
this way ?
const array =
[ 'something', '6'
, 'somethingelse', '130'
, 'carrot', '89'
, 'monkey', '57'
, 'plane', '71'
]
const range = 65
for (let index = array.length -1; index > 0; index -= 2)
{
if (Number(array[index]) < range) array.splice(index-1, 2)
}
console.log( JSON.stringify(array) )