The fastest way to remove a specific item from an array in

I have an array of numbers & I"m using the .push() method to add elements to it.

Bạn đang xem: The fastest way to remove a specific item from an array in

Is there a simple way lớn remove a specific element from an array?

I"m looking for the equivalent of something like:

array.remove(number);I have to lớn use core JavaScript. Frameworks are not allowed.



Find the index of the array element you want khổng lồ remove sầu 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.

Xem thêm: http vn viewfruit com download php

const array = <2, 5, 9>;console.log(array);const index = array.indexOf(5);if (index > -1) array.splice(index, 1);// array = <2, 9>console.log(array);

The second parameter of splice is the number of elements khổng lồ remove. lưu ý that splice modifies the array in place và 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
Edited on 2016 October

Do it immutable (original array stay unchanged)Do it with standard JavaScript functions, if your browser doesn"t support them - use polyfill

In this code example I use "array.filter(...)" function to lớn remove unwanted items from an array. This function doesn"t change the original array và creates a new one. If your browser doesn"t support this function (e.g. Internet Explorer before version 9, or Firefox before version 1.5), consider using the filter polyfill from Mozilla.

Removing công trình (ECMA-262 Edition 5 code aka oldstyle JavaScript)

var value = 3var arr = <1, 2, 3, 4, 5, 3>arr = arr.filter(function(item) return thành tựu !== value)console.log(arr)// < 1, 2, 4, 5 >

Removing cửa nhà (ECMAScript 6 code)

let value = 3let arr = <1, 2, 3, 4, 5, 3>arr = arr.filter(vật phẩm => cửa nhà !== value)console.log(arr)// < 1, 2, 4, 5 >IMPORTANT ECMAScript 6 "() => " arrow function syntax is not supported in Internet Explorer at all, Chrome before 45 version, Firefox before 22 version, và Safari before 10 version. To use ECMAScript 6 syntax in old browsers you can use BabelJS.

Removing multiple items (ECMAScript 7 code)

An additional advantage of this method is that you can remove multiple items

let forDeletion = <2, 3, 5>let arr = <1, 2, 3, 4, 5, 3>arr = arr.filter(chiến thắng => !forDeletion.includes(item))// !!! Read below about array.includes(...) support !!!console.log(arr)// < 1, 4 >IMPORTANT "array.includes(...)" function is not supported in Internet Explorer at all, Chrome before 47 version, Firefox before 43 version, Safari before 9 version, and Edge before 14 version so here is polyfill from Mozilla.

Removing multiple items (in the future, maybe)

If the "This-Binding Syntax" proposal is ever accepted, you"ll be able to lớn vày this:

// array-lib.jsexport function remove(...forDeletion) return this.filter(tòa tháp => !forDeletion.includes(item))// main.jsimport remove from "./array-lib.js"let arr = <1, 2, 3, 4, 5, 3>// :: This-Binding Syntax Proposal// using "remove" function as "virtual method"https:// without extending Array.prototypearr = arr::remove(2, 3, 5)console.log(arr)// < 1, 4 >Try it yourself in BabelJS :)