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

131
Visualizações
Javascript display all elements of array except for the specified index

Out of curiosity, is there any way to display inverted array index. Let's say we got this as our array:

var color = ["red", "green", "blue", "yellow"];
console.log(color[2]);

Of course the console will show "blue" right?

What if I want to display other than "blue"? That mean "red", "green", "yellow". Or should we use slice instead?

Thank you

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use .filter() to filter out elements. So, in this case filter out all the elements of the array which is not blue.

var color = ["red", "green", "blue", "yellow"];
console.log(color.filter(col => col !== "blue"));
about 4 years ago · Juan Pablo Isaza Relatório

0

1) You can use a filter here

var color = ["red", "green", "blue", "yellow"];
const result = color.filter((c) => c !== "blue");
console.log(result);

2) Although you can also use splice

var color = ["red", "green", "blue", "yellow"];
const newArray = [...color];
newArray.splice(2, 1);
console.log(newArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

If I got it right what you want, so show everything EXCEPT the one which's index you pass, then:

myFun = (myArray, n) => {
  return myArray.slice(0, n).concat(myArray.slice(n+1))
}
color = ["red", "green", "blue", "yellow"]
myFun(color, 2)

// ["red", "green", "yellow"]
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