admin管理员组文章数量:1023273
I would like to simulate the C# Any() method, which can be used to determine whether if a collection has any matching objects based on a lambda expression.
I used jQuery's $.grep to make things easier:
Array.prototype.any = function (expr) {
if (typeof jQuery === 'undefined')
throw new ReferenceError('jQuery not loaded');
return $.grep(this, function (x, i) {
return eval(expr);
}).length > 0;
};
var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];
console.log(foo.any('x.a === 1')); //true
console.log(foo.any('x.a === 2')); //false
I know that eval()
is bad practice for obvious reasons. But is it ok in this case, since I won't use this with anything related to some user inputs?
Can this be done without eval()
? I can't figure out a way to pass an expression to the function without evaluating it.
/
I would like to simulate the C# Any() method, which can be used to determine whether if a collection has any matching objects based on a lambda expression.
I used jQuery's $.grep to make things easier:
Array.prototype.any = function (expr) {
if (typeof jQuery === 'undefined')
throw new ReferenceError('jQuery not loaded');
return $.grep(this, function (x, i) {
return eval(expr);
}).length > 0;
};
var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];
console.log(foo.any('x.a === 1')); //true
console.log(foo.any('x.a === 2')); //false
I know that eval()
is bad practice for obvious reasons. But is it ok in this case, since I won't use this with anything related to some user inputs?
Can this be done without eval()
? I can't figure out a way to pass an expression to the function without evaluating it.
http://jsfiddle/dgGvN/
Share Improve this question asked May 14, 2013 at 13:53 JohanJohan 35.3k62 gold badges189 silver badges306 bronze badges 3- 7 Can't you pass a function instead of an expression string? That would be more js-friendly. – bfavaretto Commented May 14, 2013 at 13:55
- @bfavaretto: I think your ment makes a good answer. – nhahtdh Commented May 14, 2013 at 13:57
- @nhahtdh I was too slow, now it would be pointless given the current answers. – bfavaretto Commented May 14, 2013 at 14:04
2 Answers
Reset to default 9I suggest you take a good look at JS closures. In particular, what you did there can be done natively in JS by using the Array.some method:
[{ a: 1, b: 2 }, { a:1, b: 3 }].some(function(x) { return x.a === 1; }); // true
[{ a: 1, b: 2 }, { a:1, b: 3 }].some(function(x) { return x.a === 2; }); // false
edit: in this case we're not really using closures but rather plain simple anonymous functions...
Pass in a function:
Array.prototype.any = function (expr) {
if (typeof jQuery === 'undefined')
throw new ReferenceError('jQuery not loaded');
return $.grep(this, expr).length > 0;
};
var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];
console.log(foo.any(function(x, i){return x.a === 1})); //true
console.log(foo.any(function(x, i){return x.a === 2})); //false
I would like to simulate the C# Any() method, which can be used to determine whether if a collection has any matching objects based on a lambda expression.
I used jQuery's $.grep to make things easier:
Array.prototype.any = function (expr) {
if (typeof jQuery === 'undefined')
throw new ReferenceError('jQuery not loaded');
return $.grep(this, function (x, i) {
return eval(expr);
}).length > 0;
};
var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];
console.log(foo.any('x.a === 1')); //true
console.log(foo.any('x.a === 2')); //false
I know that eval()
is bad practice for obvious reasons. But is it ok in this case, since I won't use this with anything related to some user inputs?
Can this be done without eval()
? I can't figure out a way to pass an expression to the function without evaluating it.
/
I would like to simulate the C# Any() method, which can be used to determine whether if a collection has any matching objects based on a lambda expression.
I used jQuery's $.grep to make things easier:
Array.prototype.any = function (expr) {
if (typeof jQuery === 'undefined')
throw new ReferenceError('jQuery not loaded');
return $.grep(this, function (x, i) {
return eval(expr);
}).length > 0;
};
var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];
console.log(foo.any('x.a === 1')); //true
console.log(foo.any('x.a === 2')); //false
I know that eval()
is bad practice for obvious reasons. But is it ok in this case, since I won't use this with anything related to some user inputs?
Can this be done without eval()
? I can't figure out a way to pass an expression to the function without evaluating it.
http://jsfiddle/dgGvN/
Share Improve this question asked May 14, 2013 at 13:53 JohanJohan 35.3k62 gold badges189 silver badges306 bronze badges 3- 7 Can't you pass a function instead of an expression string? That would be more js-friendly. – bfavaretto Commented May 14, 2013 at 13:55
- @bfavaretto: I think your ment makes a good answer. – nhahtdh Commented May 14, 2013 at 13:57
- @nhahtdh I was too slow, now it would be pointless given the current answers. – bfavaretto Commented May 14, 2013 at 14:04
2 Answers
Reset to default 9I suggest you take a good look at JS closures. In particular, what you did there can be done natively in JS by using the Array.some method:
[{ a: 1, b: 2 }, { a:1, b: 3 }].some(function(x) { return x.a === 1; }); // true
[{ a: 1, b: 2 }, { a:1, b: 3 }].some(function(x) { return x.a === 2; }); // false
edit: in this case we're not really using closures but rather plain simple anonymous functions...
Pass in a function:
Array.prototype.any = function (expr) {
if (typeof jQuery === 'undefined')
throw new ReferenceError('jQuery not loaded');
return $.grep(this, expr).length > 0;
};
var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];
console.log(foo.any(function(x, i){return x.a === 1})); //true
console.log(foo.any(function(x, i){return x.a === 2})); //false
本文标签: jquerySimulate C Lambda methods in JavascriptStack Overflow
版权声明:本文标题:jquery - Simulate C# Lambda methods in Javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598532a2158318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论