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 OverflowArray.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
Use splice(startPosition, deleteCount)
array.splice(-1)
Show code snippet
var array = ['abc','def','ghi','123'];
var removed = array.splice(-1); //last item
console.log( 'array:', array );
console.log( 'removed:', removed );
Run code snippetEdit code snippet Hide Results Copy to answer Expand
javascript - Js remove element from array without change the original - Stack Overflow
typescript - Remove and update an array without mutation in Javascript - Stack Overflow
javascript - How to remove n from an array without mutating it? - Stack Overflow
why using index as key remove only the last element in React?
Array.prototype.filter will create and return a new array consisting of elements that match the predicate.
function removeByIndex(array, index) {
return array.filter(function (el, i) {
return index !== i;
});
}
Even shorter with ECMAScript 6:
var removeByIndex = (array, index) => array.filter((_, i) => i !== index);
You can use the es6 spread operator and Array.prototype.splice
var arr = [{id:1, name:'name'},{id:2, name:'name'},{id:3, name:'name'}];
let newArr = [...arr]
newArr.splice(index)
The spread operator copies the array and splice changes the contents of an array by removing or replacing existing elements and/or adding new elements
You're creating two additional arrays: one copyArr and one filtered array. There's no need for both, creating just the filtered array should work fine, eg:
setOriginalArr(
originalArr.filter(item => item !== 'plum')
);
as an example, if originalArr is in state.
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.
You can use slice() method to extract items from/to specific indexes (without mutation of original array)
let originalArr = ['apple', 'plum', 'berry'];
let sliced = originalArr.slice(1, 2)
console.log(originalArr, sliced)
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
function removeItem(array, n) {
return array.filter((x, i) => i != n)
}