Js remove element from array without change the original
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
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);