-
Star
(103)
You must be signed in to star a gist -
Fork
(29)
You must be signed in to fork a gist
-
-
Save chad3814/2924672 to your computer and use it in GitHub Desktop.
// This is from my comment here: http://wolfram.kriesing.de/blog/index.php/2008/javascript-remove-element-from-array/comment-page-2#comment-466561 | |
/* | |
* How to delete items from an Array in JavaScript, an exhaustive guide | |
*/ | |
// DON'T use the delete operator, it leaves a hole in the array: | |
var arr = [4, 5, 6]; | |
delete arr[1]; // arr now: [4, undefined, 6] | |
// DO use splice: | |
var arr = [4, 5, 6]; | |
arr.splice(1, 1); | |
// If you want to find the item to delete you can use .indexOf() | |
var arr = [4, 5, 6]; | |
var idx = arr.indexOf(5); // 1 | |
// be careful, .indexOf() will return -1 if the item is not found | |
if (idx !== -1) { | |
arr.splice(idx, 1); | |
} | |
// Not all browsers support .indexOf(), so here's a monkey-patch: | |
if (!Array.prototype.indexOf) { | |
// augment the Array prototype with an indexOf that conforms | |
// to ECMAScript5 | |
// item - this is the object we're looking for | |
// start - this is where to start looking | |
// returns the index of the item if found, otherwise -1 | |
Array.prototype.indexOf = function (item, start) { | |
start = start || 0; | |
for( ; start < this.length; start++) { | |
if (this[start] === item) { | |
return start; | |
} | |
} | |
return -1; | |
}; | |
} | |
// If you want to splice in an iterator like Array's forEach() | |
// DON'T do this: | |
var arr = [5, 6, 7, 8, 9]; | |
arr.forEach(function (item, index) { | |
if (item === 7 || item === 8) { | |
arr.splice(index, 1); | |
} | |
}); | |
// arr is now something like: [5, 6, 8, 9] | |
// DO use the third argument to the iterator function: | |
var arr = [5, 6, 7, 8, 9]; | |
arr.forEach(function (item, index, arr) { | |
if (item === 7 || item === 8) { | |
arr.splice(index, 1); | |
} | |
}); | |
// arr is now [5, 6, 9] | |
/* | |
* The 'arr' in the iterator 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 here's a monkey-patch: | |
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]; | |
} | |
}; | |
} |
nice!
Good!
Cool
thanks!
thx!
thanks!
use Array.filter
thanks! 👍
That reverse for loop is what i'm looking for! , Thanks!
very good, reverse for loop! , Thanks!
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...
*/
Awesome. Thank you so much.
Biblical!
Nice Thanks :)
Nice! Thanks!
need continue after splice in reverse for-loop
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
}
)
})
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?
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
thank you !
Thank you!
nice !