Community Page
- paulmwatson.com/journal Jump to website »
-
Subscribe -
Community
-
Top Commenters
-
Popular Threads
-
Recent Comments
- Make songbird look like spotify: http://addons.songbirdnest.com/addon/1440
- Got it, thanks Paul!
- Email me your email address so I can invite you Mike (paul@paulmwatson.com)
- Happy New Year to you, as well! I was stopping by to see if you would be willing to lend a reader a Spotify invitation. I am desperately hoping to be able try out the service. Thanks! Mike
- Nice one Jamie. Even more ironic is that that "mass production" is probably still underpaid, underage workers in some 3rd world country sweat-shop.
Jump to original thread »
I like the way you can add methods/functions/members to built in JavaScript entities. The following adds a remove method to the Array object.
Array.prototype.remove = function(item_to_remove)
{
for (var i = 0; i < this.length; i++)
%2 ... Continue reading »
Array.prototype.remove = function(item_to_remove)
{
for (var i = 0; i < this.length; i++)
%2 ... Continue reading »
2 years ago
2 years ago
And I reckon it could be used, just use this = this.filter(func);.
2 years ago
2 years ago
Maybe you could do this.clear and then this.push(this.filter blah blah
2 years ago
2 years ago
Array.prototype.remove = function(item_to_remove)
{
var index;
while((index = this.indexOf(item_to_remove)) != -1)
{
this.splice(index, 1);
}
}
That'll remove all of the items that match. For instance:
var filtered = [1, 2, 3, 4, 5, 6, 5, 7, 8, 5, 9, 10];
filtered.remove(5);
document.write(filtered);
1,2,3,4,6,7,8,9,10
2 years ago
Array.prototype.remove = function(item_to_remove)
{
var index;
while((index = this.indexOf(item_to_remove)) != -1)
{
this.splice(index, 1);
}
return this;
}
That way you can do:
var filtered = [1, 2, 3, 4, 5, 6, 5, 7, 8, 5, 9, 10].remove(5);
document.write(filtered);
2 years ago
I'm not sure if javascript also has them, but a couple of other useful Python functions on sequences are map and reduce, which behave like:
def map(fn, list):
newlist = []
for item in list:
newlist.append = fn(item)
def reduce(fn ,list, initial_arg = None):
if initial_arg:
val = initial_arg
start = 0
else:
val = list[0]
start = 1
for i in range(start, len(list)):
val = fn(val, myseq[i])
return val
I may have goofed these up as I did them off the top of my head, but hopefully you get the idea.
2 years ago
2 years ago
2 years ago