Skip to content

Instantly share code, notes, and snippets.

@huytd huytd/LinqJS
Created Apr 25, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.