Skip to content

Instantly share code, notes, and snippets.

@chad3814
Last active August 29, 2024 05:43
Show Gist options
  • Save chad3814/2924672 to your computer and use it in GitHub Desktop.
Save chad3814/2924672 to your computer and use it in GitHub Desktop.
deleting array items in javascript with forEach() and splice()
// If you want to splice in an iterator like Array's forEach() you can do it like this:
var arr = [4, 5, 6];
arr.forEach(function (item, index, arr) {
if (item === 5) {
arr.splice(index, 1);
}
});
/*
The 'arr' in the lambda is a clone of the original arr, and at the end of the forEach, the original gets its length and all elements set to the clone's length and elements.
*/
// Of course not all browsers Array implement forEach so:
if (!Array.prototype.forEach) {
// augment the Array prototype with a forEach that conforms
// to ECMAScript5
// iterator - a function with signature (item, index, array)
// context - an optional context to call the iterator, 'this'
// will be set to it inside of the iterator
Array.prototype.forEach = function (iterator, context) {
var arr = (new Array()).concat(this); // clone this
var i;
for(i = 0; i < this.length; i++) { // iterate over this, but use the clone
iterator.call(context, arr[i], i, arr);
}
this.length = arr.length; // reset this to the clone's items
for(i = 0; i < this.length; i++) {
this[i] = arr[i];
}
};
}
@baio
Copy link

baio commented Mar 23, 2015

nice !

@lumixraku
Copy link

nice!

@tbela99
Copy link

tbela99 commented Jun 25, 2015

Good!

@jason-engage
Copy link

Cool

@earfish90807
Copy link

thanks!

@yooungt13
Copy link

thx!

@stansamyn
Copy link

thanks!

@yodfz
Copy link

yodfz commented Jul 28, 2016

use Array.filter

@nurhadimaulana2309
Copy link

thanks! 👍

@WilliamAnputra
Copy link

That reverse for loop is what i'm looking for! , Thanks!

@hsai2696
Copy link

very good, reverse for loop! , Thanks!

@tdiprima
Copy link

Very nice, thanks! By the way, there's an unfortunate "space" between the / and the * on Line 73... the space needs to go away in order for this to work.

Like...

/*
 * Comment...
 */

or

/**
 * Comment...
 */

@jawad-aziz-farhad
Copy link

Awesome. Thank you so much.

@LiamKlyneker
Copy link

Biblical!

@vinoth8693
Copy link

Nice Thanks :)

@live106
Copy link

live106 commented Feb 26, 2019

Nice! Thanks!

@kawais
Copy link

kawais commented Sep 10, 2019

need continue after splice in reverse for-loop

@zolotarev
Copy link

zolotarev commented Mar 10, 2021

My case:

cash_menu.map((i, i_index) => {
    cash_menu[i_index].items = cash_menu[i_index].items.filter(item => {
            console.log(item)
            return item.Price !== 0
        }
    )
})

@chad3814
Copy link
Author

Very nice, thanks! By the way, there's an unfortunate "space" between the / and the * on Line 73... the space needs to go away in order for this to work.

Nothing like fixing something three years later, right?

@chad3814
Copy link
Author

need continue after splice in reverse for-loop

only if you are doing other things in the loop that you don't want to happen if you remove the item

@sorie
Copy link

sorie commented Jun 25, 2021

thank you !

@farithadnan
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment