I assume you used splice something like this?

Copyfor (var i = 0; i < arrayOfObjects.length; i++) {
    var obj = arrayOfObjects[i];

    if (listToDelete.indexOf(obj.id) !== -1) {
        arrayOfObjects.splice(i, 1);
    }
}

All you need to do to fix the bug is decrement i for the next time around, then (and looping backwards is also an option):

Copyfor (var i = 0; i < arrayOfObjects.length; i++) {
    var obj = arrayOfObjects[i];

    if (listToDelete.indexOf(obj.id) !== -1) {
        arrayOfObjects.splice(i, 1);
        i--;
    }
}

To avoid linear-time deletions, you can write array elements you want to keep over the array:

Copyvar end = 0;

for (var i = 0; i < arrayOfObjects.length; i++) {
    var obj = arrayOfObjects[i];

    if (listToDelete.indexOf(obj.id) === -1) {
        arrayOfObjects[end++] = obj;
    }
}

arrayOfObjects.length = end;

and to avoid linear-time lookups in a modern runtime, you can use a hash set:

Copyconst setToDelete = new Set(listToDelete);
let end = 0;

for (let i = 0; i < arrayOfObjects.length; i++) {
    const obj = arrayOfObjects[i];

    if (setToDelete.has(obj.id)) {
        arrayOfObjects[end++] = obj;
    }
}

arrayOfObjects.length = end;

which can be wrapped up in a nice function:

Copyconst filterInPlace = (array, predicate) => {
    let end = 0;

    for (let i = 0; i < array.length; i++) {
        const obj = array[i];

        if (predicate(obj)) {
            array[end++] = obj;
        }
    }

    array.length = end;
};

const toDelete = new Set(['abc', 'efg']);

const arrayOfObjects = [{id: 'abc', name: 'oh'},
                        {id: 'efg', name: 'em'},
                        {id: 'hij', name: 'ge'}];

filterInPlace(arrayOfObjects, obj => !toDelete.has(obj.id));
console.log(arrayOfObjects);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

If you don’t need to do it in place, that’s Array#filter:

Copyconst toDelete = new Set(['abc', 'efg']);
const newArray = arrayOfObjects.filter(obj => !toDelete.has(obj.id));
Answer from Ry- on Stack Overflow
Top answer
1 of 15
191

I assume you used splice something like this?

Copyfor (var i = 0; i < arrayOfObjects.length; i++) {
    var obj = arrayOfObjects[i];

    if (listToDelete.indexOf(obj.id) !== -1) {
        arrayOfObjects.splice(i, 1);
    }
}

All you need to do to fix the bug is decrement i for the next time around, then (and looping backwards is also an option):

Copyfor (var i = 0; i < arrayOfObjects.length; i++) {
    var obj = arrayOfObjects[i];

    if (listToDelete.indexOf(obj.id) !== -1) {
        arrayOfObjects.splice(i, 1);
        i--;
    }
}

To avoid linear-time deletions, you can write array elements you want to keep over the array:

Copyvar end = 0;

for (var i = 0; i < arrayOfObjects.length; i++) {
    var obj = arrayOfObjects[i];

    if (listToDelete.indexOf(obj.id) === -1) {
        arrayOfObjects[end++] = obj;
    }
}

arrayOfObjects.length = end;

and to avoid linear-time lookups in a modern runtime, you can use a hash set:

Copyconst setToDelete = new Set(listToDelete);
let end = 0;

for (let i = 0; i < arrayOfObjects.length; i++) {
    const obj = arrayOfObjects[i];

    if (setToDelete.has(obj.id)) {
        arrayOfObjects[end++] = obj;
    }
}

arrayOfObjects.length = end;

which can be wrapped up in a nice function:

Copyconst filterInPlace = (array, predicate) => {
    let end = 0;

    for (let i = 0; i < array.length; i++) {
        const obj = array[i];

        if (predicate(obj)) {
            array[end++] = obj;
        }
    }

    array.length = end;
};

const toDelete = new Set(['abc', 'efg']);

const arrayOfObjects = [{id: 'abc', name: 'oh'},
                        {id: 'efg', name: 'em'},
                        {id: 'hij', name: 'ge'}];

filterInPlace(arrayOfObjects, obj => !toDelete.has(obj.id));
console.log(arrayOfObjects);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

If you don’t need to do it in place, that’s Array#filter:

Copyconst toDelete = new Set(['abc', 'efg']);
const newArray = arrayOfObjects.filter(obj => !toDelete.has(obj.id));
2 of 15
140

You can remove an item by one of its properties without using any 3rd party libs like this:

Copyvar removeIndex = array.map(item => item.id).indexOf("abc");

~removeIndex && array.splice(removeIndex, 1);
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to remove an element from an array by id in javascript
How to Remove an Element From an Array by ID in JavaScript
November 24, 2022 - We can also remove an element from an array by ID with the filter() method. We call filter() on the array, passing a callback that returns true for every element in that array apart from the object with the specified ID.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › remove-array-element-based-on-object-property-in-javascript
Remove Array Element Based on Object Property in JavaScript - GeeksforGeeks
July 23, 2025 - The code removes an object with id = 2 from the array using the reduce() method. It iterates through the array, pushing only the objects that don’t match the specified id into a new array, which becomes the updated arr.
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-remove-element-from-array-by-id-9c548f2aa27b
Remove an Element from an Array by ID in JavaScript | JavaScript in Plain English
July 22, 2024 - We can also remove an element from an array by ID with the filter() method. We call filter() on the array, passing a callback that returns true for every element in that array apart from the object with the specified ID.
🌐
GitHub
gist.github.com › scottopolis › 6e35cf0d53bae81e6161662e6374da04
Remove object from array of objects in Javascript · GitHub
remove a single object from a array of objects · is, apps.splice( apps.findIndex(a => a.id === 37) , 1); Copy link · Thank you all masters. Copy link · Exactly the thing I was looking for. Tks, bro. Copy link · Awesome thanks · Copy link · Thanks a lot, It was Great.
🌐
TutorialsPoint
tutorialspoint.com › search-by-id-and-remove-object-from-json-array-in-javascript
Search by id and remove object from JSON array in JavaScript
const arr = [ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwick", type: "comedy"}, {id: "3", name: "X-Men", type: "action"}, {id: "4", name: "Ordinary People", type: "drama"}, {id: "5", name: "Billy Elliot", type: "drama"}, {id: "6", name: "Toy Story", type: "children"} ]; const removeById = (arr, id) => { const requiredIndex = arr.findIndex(el => { return el.id === String(id); }); if(requiredIndex === -1){ return false; }; return !!arr.splice(requiredIndex, 1); }; removeById(arr, 5); console.log(arr);
🌐
EyeHunts
tutorial.eyehunts.com › home › remove object from array javascript by id | example code
Remove object from array JavaScript by id | Example code
May 5, 2023 - <script> var listToDelete = ['abc', 'efg']; var arrayOfObjects = [{id:'abc',name:'oh'}, {id:'efg',name:'em'}, {id:'hij',name:'ge'}] for (var i = 0; i < arrayOfObjects.length; i++) { var obj = arrayOfObjects[i]; if (listToDelete.indexOf(obj.id) !== -1) { arrayOfObjects.splice(i, 1); i--; } } console.log(arrayOfObjects) </script>
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-object-from-array-of-objects-using-javascript
How to remove object from array of objects using JavaScript ? - ...
July 12, 2025 - The approach using array.filter() with a for...of loop involves iterating over an array of objects with for...of and applying filter() to exclude specific objects based on a condition.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-object-from-array-by-value
Remove Object from an Array by its Value in JavaScript | bobbyhadz
Copied!const arr = [{id: 1}, {id: 3}, {id: 5}]; const indexOfObject = arr.findIndex(object => { return object.id === 3; }); console.log(indexOfObject); // 👉️ 1 · The next step is to use the Array.splice() method to remove the object at ...
Top answer
1 of 15
12

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...)

2 of 15
11

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;
}
🌐
Medium
sonjoybarmon.medium.com › delete-an-element-from-an-array-in-javascript-and-react-js-using-id-611322ee38cf
Delete an element from an array in JavaScript and React js using ID | by Sonjoy Chandra Barman | Medium
December 21, 2022 - It also includes a Delete button for each object, which when clicked will call the deleteById() function and pass in the object's id as an argument. The deleteById() the function then removes the object from the array using the splice() method, ...
🌐
Ultimate Courses
ultimatecourses.com › blog › remove-specific-item-from-array-javascript
Removing Items from an Array in JavaScript - Ultimate Courses
In this article, we’ll explore a few different ways to remove an item from an array in JavaScript. I will also show you mutable and...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript remove object from array
How to Remove Object From an Array in JavaScript | Delft Stack
February 2, 2024 - First, create an array with objects as done in the methods above. Then call the filter() function with the array variable. Specify item as the argument of the arrow function and return the elements of the array with the expression item.id !
🌐
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 - The shift method removes the first item of the array. It also returns the removed element. You can remove the element at any index by using the splice method.
🌐
CoreUI
coreui.io › blog › how-to-remove-element-from-javascript-array
How to Remove Elements from a JavaScript Array · CoreUI
February 13, 2025 - In many real-world applications dealing with JavaScript objects and arrays, you might need to remove array elements based on user input or a dynamically generated condition. By using methods like splice or filter, you can handle different methods of removal gracefully.
Top answer
1 of 16
1042

You can use several methods to remove item(s) from an Array:

//1
someArray.shift(); // first element removed
//2
someArray = someArray.slice(1); // first element removed
//3
someArray.splice(0, 1); // first element removed
//4
someArray.pop(); // last element removed
//5
someArray = someArray.slice(0, someArray.length - 1); // last element removed
//6
someArray.length = someArray.length - 1; // last element removed

If you want to remove element at position x, use:

someArray.splice(x, 1);

Or

someArray = someArray.slice(0, x).concat(someArray.slice(-x));

Reply to the comment of @chill182: you can remove one or more elements from an array using Array.filter, or Array.splice combined with Array.findIndex (see MDN).

See this Stackblitz project or the snippet below:

// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray();
let noJohn = someArray.filter( el => el.name !== "John" ); 
log(`let noJohn = someArray.filter( el => el.name !== "John")`,
  `non destructive filter [noJohn] =`, format(noJohn));
log(`**someArray.length ${someArray.length}`);

// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray();
someArray2 = someArray2.filter( el => el.name !== "John" );
log("", 
  `someArray2 = someArray2.filter( el => el.name !== "John" )`,
  `destructive filter/reassign John removed [someArray2] =`, 
  format(someArray2));
log(`**someArray2.length after filter ${someArray2.length}`);

// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray();
someArray3.splice(someArray3.findIndex(v => v.name === "Kristian"), 1);
someArray3.splice(someArray3.findIndex(v => v.name === "John"), 1);
log("",
  `someArray3.splice(someArray3.findIndex(v => v.name === "Kristian"), 1),`,
  `destructive splice /w findIndex Brian remains [someArray3] =`, 
  format(someArray3));
log(`**someArray3.length after splice ${someArray3.length}`);

// if you're not sure about the contents of your array, 
// you should check the results of findIndex first
let someArray4 = getArray();
const indx = someArray4.findIndex(v => v.name === "Michael");
someArray4.splice(indx, indx >= 0 ? 1 : 0);
log("", `someArray4.splice(indx, indx >= 0 ? 1 : 0)`,
  `check findIndex result first [someArray4] = (nothing is removed)`,
  format(someArray4));
log(`**someArray4.length (should still be 3) ${someArray4.length}`);

// -- helpers -- 
function format(obj) {
  return JSON.stringify(obj, null, " ");
}

function log(...txt) {
  document.querySelector("pre").textContent += `${txt.join("\n")}\n`
}

function getArray() {
  return [ {name: "Kristian", lines: "2,5,10"},
           {name: "John", lines: "1,19,26,96"},
           {name: "Brian", lines: "3,9,62,36"} ];
}
<pre>
**Results**

</pre>

2 of 16
334

The clean solution would be to use Array.filter:

var filtered = someArray.filter(function(el) { return el.Name != "Kristian"; }); 

The problem with this is that it does not work on IE < 9. However, you can include code from a Javascript library (e.g. underscore.js) that implements this for any browser.

🌐
DEV Community
dev.to › sharmaprash › how-can-i-remove-a-specific-item-from-an-array-in-javascript-52j3
How can I remove a specific item from an array in JavaScript? - DEV Community
September 7, 2024 - If you need to remove an object from an array based on a condition: const array = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' } ]; const index = array.findIndex(item => item.id === 2); if (index !== -1) { array.splice(index, 1); } console.log(array); // [{ id: 1, name: 'Alice' }, { id: 3, name: 'Charlie' }]
🌐
Reddit
reddit.com › r/learnjavascript › deleting objects in an array of objects with undefined or null keys.
r/learnjavascript on Reddit: Deleting objects in an array of objects with undefined or null keys.
August 16, 2021 -

Need some help asap, please. I'm trying to loop through this array of objects and remove the entire object if the ID is '' or "ID".

That is, I need to get from this...

"newArr" [
{ ID: '16', Name: 'Abraham Lincoln', Start: '1861', End: '1865' },
{ ID: '', Name: undefined, Start: undefined, End: undefined },
{ ID: 'ID', Name: 'Name', Start: 'Start', End: 'End' },
{ ID: '15', Name: 'James Buchanan', Start: '1857', End: '1861' },
{ ID: '', Name: undefined, Start: undefined, End: undefined }
]

...to this:

[
{ ID: '16', Name: 'Abraham Lincoln', Start: '1861', End: '1865' },
{ ID: '15', Name: 'James Buchanan', Start: '1857', End: '1861' }
]

I either get "typeError: Cannot read properties of undefined (reading 'ID')" or it just does nothing and the object remains. Here's what I've tried:

for (let i=0; i<newArr.length; i++) {
     for (let j=0; j<newArr[i].length; j++) {
      if (newArr[i][j] == null || undefined || '') delete newArr[i];

or

for (let i=0; i<newArr.length; i++) {  
     Object.keys(newArr[i]).forEach((k) => newArr[i][k] == null || undefined || '' && delete newArr[i]);