Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda