-
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.
// 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]; | |
} | |
}; | |
} |
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 !