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

101
Visualizações
Javascript slice array after i + 1 value

I'm trying to slice an array at i + 1 from the last null value.

The array is sorted to have null values first.

This is how it should behave:

[null, null, a, b, c] => [null, null, a, b]

[null, a, b, c] => [null, a, b]

[null, null] => [null, null]

I'm doing something like this but I'm not sure how to handle the null only arrays.

let newArray: = [];

for (let i = 0; i < array.length; i++) {
  if (array[i] !== null) {
    newArray = array.slice(0, i + 1);
    i++;
  }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There are some errors in your code that might be causing the unexpected behaviour.

  • Since you have i++ inside of the for loop head you don't need to include it in the body.
  • Since the second parameter of slice is non-inclusive, based on your test conditions you actually want to slice to i + 2;

Full code:

function slice(array) {
    for (let i = 0; i < array.length; i++) {
        if (array[i] !== null) return array.slice(0, i + 2);
    }
    return array;
}

console.log(slice([null, null, 'a', 'b', 'c'])); // => [null, null, a, b]
console.log(slice([null, 'a', 'b', 'c'])); // => [null, a, b]
console.log(slice([null, null])); // => [null, null]

about 4 years ago · Juan Pablo Isaza Relatório

0

You could take a closure over a counter for the next values and filter the array.

const
    slice = array => array.filter(
        (c => v => v === null || c && c--)
        (2)
    );

console.log(...slice([null, null, 'a', 'b', 'c', 'd', 'e'])); // [null, null, a, b]
console.log(...slice([null, null, 'a', 'b', 'c'])); // [null, null, a, b]
console.log(...slice([null, 'a', 'b', 'c'])); // [null, a, b]
console.log(...slice([null, null])); // [null, null]

about 4 years ago · Juan Pablo Isaza Relatório

0

Looks to me, from your examples, you only needed to sliced out an array without the last element of the array. You wanna change the first array with nulls, a, b and c to another with the same nulls, a and b.

[null, null, a, b, c] => [null, null, a, b]

Then if that's the case, why not just pop the array.

let arr = [null, null, a, b, c];
arr.pop();

arr is now [null, null, a, b];

For the special case in which only non null values should be removed, and thereby returning same array if it contains all nulls, you may just pop, inspect, and if null push back to original array again.

let arr = [null, null, a, b, c];
if(arr.pop() === null) arr.push(null);

And that's all!

You should please clarify what you mean by i + 1. Sounds like you want to slice a new array with all nulls, and one element after it. But I cant answer to that because I'm not sure.

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