Categories
JavaScript Answers

How to select all other values in an array except the ith element with JavaScript?

Spread the love

To select all other values in an array except the ith element with JavaScript, we use the filter method.

For instance, we write

const items = [1, 2, 3, 4, 5, 6];
const current = 2;
const itemsWithoutCurrent = items.filter((x) => {
  return x !== current;
});

to call items.filter with a callback that checks if x isn’t equal to current and return all the elements in items where x isn’t equal to current.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *