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

130
Views
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 answers
Answer question

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 Report

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 Report

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