Skip to content

Instantly share code, notes, and snippets.

@oxpa
Last active August 29, 2015 14:18
Show Gist options
  • Save oxpa/b1091e0f2baf06b46269 to your computer and use it in GitHub Desktop.
Save oxpa/b1091e0f2baf06b46269 to your computer and use it in GitHub Desktop.
some js additions
Array.prototype.update = function(list) {
if (list instanceof Array) {
for (n of list) {
var updated = false;
for(var i=0; i< this.length; i++) {
if (n.name == this[i].name) {this[i] = n; updated = true; break;}
};
if (!updated) this.push(n);
}
return this;
}
};
Array.prototype.by_name = function(attr, val) {
var rv = Array();
if (attr && val) {
for (e of this){
if ( attr in e && e[attr]==val ) {
rv.push(e);
}
}
}
return rv;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment