Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda