How do I remove a specific value from an array?

(Comments)

How do I remove a specific value from an array? Something like:

array.remove(value);

I have to use core JavaScript. Frameworks are not allowed.

Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
  array.splice(index, 1); // 2nd parameter means remove one item only
}

// array = [2, 9]
console.log(array); 

The second parameter of splice is the number of elements to remove. Note that splice modifies the array in place and returns a new array containing the elements that have been removed.


For the reason of completeness, here are functions. The first function removes only a single occurrence (i.e. removing the first match of 5 from [2,5,9,1,5,8,5]), while the second function removes all occurrences:

function removeItemOnce(arr, value) {
  var index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}

function removeItemAll(arr, value) {
  var i = 0;
  while (i < arr.length) {
    if (arr[i] === value) {
      arr.splice(i, 1);
    } else {
      ++i;
    }
  }
  return arr;
}
// Usage
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,5], 5))

In TypeScript, these functions can stay type-safe with a type parameter:

function removeItem<T>(arr: Array<T>, value: T): Array<T> { 
  const index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}
Currently unrated

Comments

Riddles

22nd Jul- 2020, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20 Oct- 2019, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20Aug- 2019, by: Editor in Chief
524 Shares 4 Comments
10Aug- 2019, by: Editor in Chief
424 Shares 4 Comments
Generic placeholder image
10Aug- 2015, by: Editor in Chief
424 Shares 4 Comments

More News  »

Why being minimalist good for your money

Recent news

Why being the minimalist is good for your money

read more
2 weeks, 6 days ago

a story which relates the events of your life from past, present and future to the life cycle of a star

Recent news

What will ChatGPT answer if thre is a question? Write a story which relates the events of your life from past, present and future to the life cycle of a star. Use also those elements which could symbolize something or anything in your story.​

read more
1 month ago

Still about chatGPT and how it can pass the school exam

Recent news

ChatGPT passes exams from law and business schools

ChatGPT is trained on vast amounts of online data in order to generate responses to user prompts. While it has gained traction among users, it has also raised some concerns about inaccuracies and its potential to perpetuate biases and spread misinformation.

read more
1 month, 3 weeks ago

How to get predicted value from Stata

Recent news

How to Obtain Predicted Values and Residuals in Stata


Linear regression is a method we can use to understand the relationship between one or more explanatory variables and a response variable.

read more
1 month, 3 weeks ago

List of actor and actrees that left hollywood for the daily job

Recent news

These Former Stars Quit Hollywood for Regular Day Jobs

Stacey Dash ©Eugene Powers/Shutterstock.comStacey Dash ©Eugene Powers/Shutterstock.com

While some celebrities enjoy fame and fortune, others prefer to take the cash and then return to their normal lives, or pursue a different dream. Whether this departure from the limelight is temporary or for good, these former A-listers are now working day jobs that more closely resemble our own.

read more
1 month, 3 weeks ago

How do I remove a specific value from an array?

Recent news

How do I remove a specific value from an array? Something like:

read more
1 month, 3 weeks ago

Meet the real figure of Viking season 2 Valhalla

Recent news

Meet the real historical figures of Vikings Valhalla season 2

The historical drama is back with some new faces from the Viking Age.

The series was inspired by mythology and Viking sagas and is set 100 years after the events of the original Amazon Prime Video series.

read more
1 month, 4 weeks ago

What's the difference between gen and egen in Stata

Recent news

Is there a reason why there are two different commands to generate a new variable?

read more
2 months ago

More News »

Generic placeholder image

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without