Skip to content

Instantly share code, notes, and snippets.

@huytd
Created April 25, 2014 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/11276435 to your computer and use it in GitHub Desktop.
Save huytd/11276435 to your computer and use it in GitHub Desktop.
Linq for Javascript
/* LinQ for Javascript
* Author: Huy Tran - Email: kingbazoka@gmail.com
*/
/////////////////////////////////
Array.prototype.first = function(condition) {
return this[0];
}
Array.prototype.last = function(condition) {
return this[this.length-1];
}
Array.prototype.where = function(condition) {
var self = this;
var result = [];
for (var i = 0; i < self.length; i++) {
if ((typeof condition == "function") && (condition(self[i]))) {
result.push(self[i]);
}
}
return result;
}
////////////////////////////////////////////////
var arr = [];
arr.push({ name: "obj1", flag: true });
arr.push({ name: "obj2", flag: false });
arr.push({ name: "obj3", flag: false });
arr.push({ name: "obj4", flag: true });
var r = arr.where(function(i){return i.flag == true});
console.log(r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment