Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
How do I use a "range" to find a number in an array, then remove that index, and the one immediately the left of it?

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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

You can easily achive the result using simple for loop

const array = [
  "something",
  "6",
  "somethingelse",
  "130",
  "carrot",
  "89",
  "monkey",
  "57",
  "plane",
  "71",
];

const result = [], range = [0, 65];
for (let i = 0; i < array.length; i += 2) {
  const str = array[i];
  const num = array[i + 1];

  if (range[0] < num && num > range[1]) result.push(str, num);
}
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You could iterate all indices and check the value and remove, if necessary.

const
    remove = (array, [lower, upper]) => {
        let i = 0;
        
        while (i < array.length) {
            if (+array[i + 1] >= lower && +array[i + 1] <= upper) {
                array.splice(i, 2);
                continue;
            }        
            i += 2;
        }
        return array;
    },
    array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];

remove(array, [0, 65]);

console.log(array);

If you do not need the same object reference from array, you could filter the array.

const
    remove = (array, [lower, upper]) => array.filter((del => (_, i, { [ i + 1] : v }) => {
        if (del) return del = false;
        if (i % 2 === 0 && +v >= lower && +v <= upper) {
            del = true;
            return false;
        }
        return true;
    })()),

    array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];

console.log(remove(array, [0, 65]));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!