Array.prototype.pop() by JavaScript convention.

let fruit = ['apple', 'orange', 'banana', 'tomato'];
let popped = fruit.pop();

console.log(popped); // "tomato"
console.log(fruit); // ["apple", "orange", "banana"]
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Stuart Kershaw on Stack Overflow
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-remove-an-element-from-a-javascript-array-removing-a-specific-item-in-js
How to Remove an Element from a JavaScript Array โ€“ Removing a Specific Item in JS
August 31, 2022 - In some cases it might be appropriate to mutate the original array. In these cases you can also use one of the following mutating methods. ... You can remove the last item of an array with Array.prototype.pop().
Discussions

javascript - Js remove element from array without change the original - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
typescript - Remove and update an array without mutation in Javascript - Stack Overflow
Calling .filter on an array doesn't mutate the array it was called on - it returns a new, separate array, so it's safe to use in React without cloning the original array beforehand. More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - How to remove n from an array without mutating it? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
why using index as key remove only the last element in React?
Only ever use index when you 100% know data will not change in real time. More on reddit.com
๐ŸŒ r/react
12
6
November 15, 2022
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ pop
Array.prototype.pop() - JavaScript - MDN Web Docs
In case you want the value of this to be the same, but return a new array with the last element removed, you can use arr.slice(0, -1) instead. The pop() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, ...
๐ŸŒ
Medium
medium.com โ€บ @iamdarius โ€บ 4-ways-to-remove-the-last-element-from-an-array-in-javascript-17749b12be0c
Learn 4 Ways to Remove the Last Element from an Array in JavaScript | by Darius Moore | Medium
September 8, 2022 - Here, we simply begin passing in 0 as our first argument (which determines where to start the slice) and arr.length โ€” 1 as our last argument (the index of the first element to exclude from the returned array).
๐ŸŒ
Peanutbutterjavascript
peanutbutterjavascript.com โ€บ posts โ€บ update-arrays-without-mutating-the-original
Update arrays without mutating the original
July 29, 2020 - If you want to remove a specific element from the array, you can do this by using the Array.prototype.filter method. const original = [1,2, 'x', 3] const toDelete = 'x' const newArray = original.filter(item => item !== toDelete) I hope you found this resource useful when working with Immutable ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-splice-an-array-without-mutating-the-original-array
How to Splice an Array Without Mutating the Original Array? | GeeksforGeeks
November 19, 2024 - The combination of slice() and concat() methods allows you to remove elements from an array without mutating the original by creating a new array.
Find elsewhere
๐ŸŒ
Becomebetterprogrammer
becomebetterprogrammer.com โ€บ remove-last-element-from-an-array-in-typescript-javascript
Remove Last Element From an Array in TypeScript/JavaScript - Become A Better Programmer
August 21, 2022 - Given the shortcomings of the pop() method due to its mutable property, some alternatives exist to achieve the same goal. In the upcoming sections, we will use some array methods that do not mutate the array while removing an arrayโ€™s last element.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ splice
Array.prototype.splice() - JavaScript - MDN Web Docs
The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced().
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ array โ€บ remove elements from array
Remove elements from a JavaScript array without mutating it - 30 seconds of code
October 24, 2023 - Oftentimes, this isn't what you really want, so let's take a look at how we can implement a non-mutating version of Array.prototype.splice(). At its core, Array.prototype.splice() behaves as follows: Items between the start of the array and the given index are kept intact. Starting at the given index, the specified number of items are removed. The given items, if any, are inserted after the given index. The rest of the items are kept intact. Having broken down this behavior, we can easily see that we can implement it using Array.prototype.slice() and Array.prototype.concat().
๐ŸŒ
Jaketrent
jaketrent.com โ€บ post โ€บ remove-array-element-without-mutating
Remove an Array Element Without Mutation - Jake Trent
August 18, 2017 - To remove elements from an array in an immutable way, array copying is required. You need a new data structure because you cannot change the first -- that would be mutation.
๐ŸŒ
DEV Community
dev.to โ€บ smpnjn โ€บ removing-the-last-element-of-an-array-in-javascript-7ga
Removing the last element of an array in Javascript - DEV Community
November 6, 2022 - Finally, another way to accomplish this without changing the original array is to use slice (not to be confused with splice). slice() differs from both pop() and splice() in that it makes a shallow copy of the original array.
๐ŸŒ
IQCode
iqcode.com โ€บ code โ€บ javascript โ€บ how-to-replace-array-element-in-javascript-without-mutation
how to replace array element in javascript without mutation Code Example
October 7, 2021 - function replaceAt(array, index, value) { const ret = array.slice(0); ret[index] = value; return ret; } const newArray = replaceAt(ite...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-remove-the-last-item-from-an-array
Remove the last Item From an Array in JavaScript
November 18, 2024 - The array length property in JavaScript ... arrays. To remove the last element from an array, you can use the spread operator along with array destructuring....
Top answer
1 of 2
20

You could use filter or reduce, or copy the array first by using slice, and then perform splice.

Personally, I like filter for its simplicity, and clear intention

filter

function removeItem(array, n) {
  return array.filter((elem, i) => i !== n);
}

const original = [1,2,3,4];

console.log(removeItem(original, 1));
console.log(original);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

reduce

function removeItem (array, n) {
  return array.reduce((result, elem, i) => {
    if (i !== n) result.push(elem);
    return result;
  }, [])
}

const original = [1,2,3,4];

console.log(removeItem(original, 1));
console.log(original);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

slice and splice

function removeItem(array, n) {
  const result = array.slice();
  result.splice(n, 1);
  return result;
}

const original = [1,2,3,4];

console.log(removeItem(original, 1));
console.log(original);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Performance Test

https://jsperf.com/so53833297

Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

2 of 2
2
function removeItem(array, n) {
    return array.filter((x, i) => i != n)
}