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);
Answer from c.P.u1 on Stack OverflowArray.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
Videos
What a shame you have an array of integers, not an object where the keys are string equivalents of these integers.
I've looked through a lot of these answers and they all seem to use "brute force" as far as I can see. I haven't examined every single one, apologies if this is not so. For a smallish array this is fine, but what if you have 000s of integers in it?
Correct me if I'm wrong, but can't we assume that in a key => value map, of the kind which a JavaScript object is, that the key retrieval mechanism can be assumed to be highly engineered and optimised? (NB: if some super-expert tells me that this is not the case, I can suggest using ECMAScript 6's Map class instead, which certainly will be).
I'm just suggesting that, in certain circumstances, the best solution might be to convert your array to an object... the problem being, of course, that you might have repeating integer values. I suggest putting those in buckets as the "value" part of the key => value entries. (NB: if you are sure you don't have any repeating array elements this can be much simpler: values "same as" keys, and just go Object.values(...) to get back your modified array).
So you could do:
Copyconst arr = [ 1, 2, 55, 3, 2, 4, 55 ];
const f = function( acc, val, currIndex ){
// We have not seen this value before: make a bucket... NB: although val's typeof is 'number',
// there is seamless equivalence between the object key (always string)
// and this variable val.
! ( val in acc ) ? acc[ val ] = []: 0;
// Drop another array index in the bucket
acc[ val ].push( currIndex );
return acc;
}
const myIntsMapObj = arr.reduce( f, {});
console.log( myIntsMapObj );
Output:
Object [ <1 empty slot>, Array1, Array[2], Array1, Array1, <5 empty slots>, 46 moreโฆ ]
It is then easy to delete all the numbers 55.
Copydelete myIntsMapObj[ 55 ]; // Again, although keys are strings this works
You don't have to delete them all: index values are pushed into their buckets in order of appearance, so (for example):
CopymyIntsMapObj[ 55 ].shift(); // And
myIntsMapObj[ 55 ].pop();
will delete the first and last occurrence respectively. You can count frequency of occurrence easily, replace all 55s with 3s by transferring the contents of one bucket to another, etc.
Retrieving a modified int array from your "bucket object" is slightly involved but not so much: each bucket contains the index (in the original array) of the value represented by the (string) key. Each of these bucket values is also unique (each is the unique index value in the original array): so you turn them into keys in a new object, with the (real) integer from the "integer string key" as value... then sort the keys and go Object.values( ... ).
This sounds very involved and time-consuming... but obviously everything depends on the circumstances and desired usage. My understanding is that all versions and contexts of JavaScript operate only in one thread, and the thread doesn't "let go", so there could be some horrible congestion with a "brute force" method: caused not so much by the indexOf ops, but multiple repeated slice/splice ops.
Addendum If you're sure this is too much engineering for your use case surely the simplest "brute force" approach is
Copyconst arr = [ 1, 2, 3, 66, 8, 2, 3, 2 ];
const newArray = arr.filter( number => number !== 3 );
console.log( newArray )
(Yes, other answers have spotted Array.prototype.filter...)
Remove one value, using loose comparison, without mutating the original array, ES6
Copy/**
* Removes one instance of `value` from `array`, without mutating the original array. Uses loose comparison.
*
* @param {Array} array Array to remove value from
* @param {*} value Value to remove
* @returns {Array} Array with `value` removed
*/
export function arrayRemove(array, value) {
for(let i=0; i<array.length; ++i) {
if(array[i] == value) {
let copy = [...array];
copy.splice(i, 1);
return copy;
}
}
return array;
}
The easiest way is using shift(). If you have an array, the shift function shifts everything to the left.
var arr = [1, 2, 3, 4];
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]
Just use arr.slice(startingIndex, endingIndex).
If you do not specify the endingIndex, it returns all the items starting from the index provided.
In your case arr=arr.slice(1).
From my understanding the reason why removing the last element is O(1) is because you don't need to shift the array in memory. You simply remove the last element and leave the old space empty. So why is it that if you remove the first element that the Array HAS to to shift in memory (making it O(n))?
I don't understand the reasoning, if we are okay with leaving empty space in memory at the end of an array and not shifting all the other things surrounding the array in memory. Then why do we have to shift the array in memory if there is space that the start?
I am not understanding, if it's because the memory is trying to stay compact and no empty spaces are allowed. Then why don't all the other stuff in memory be shifted to the left after new space was cleared once we removed the last element from the array?