How to correct following code for desired output,
var Data = [{ "A": -27, "B": -39 }, { "A": 28, "B": 0}]
var filter = "x[A]==28";
var findItem = Enumerable.From(Data)
.Where(function (x) { return filter ; })
.ToArray();
alert(findItem.length);
$.each(findItem, function (i, value) {
alert(value["A"]);
});
It should give me one value A:28 or plete one record { "A": 28, "B": 0}, why i am getting two values, How to get correct results ?
using "linq.js" from following path: [ .js ]
code at JSfiddle: /
How to correct following code for desired output,
var Data = [{ "A": -27, "B": -39 }, { "A": 28, "B": 0}]
var filter = "x[A]==28";
var findItem = Enumerable.From(Data)
.Where(function (x) { return filter ; })
.ToArray();
alert(findItem.length);
$.each(findItem, function (i, value) {
alert(value["A"]);
});
It should give me one value A:28 or plete one record { "A": 28, "B": 0}, why i am getting two values, How to get correct results ?
using "linq.js" from following path: [ https://raw.github./gist/1175460/fb7404d46cab20e31601740ab8b35d99a584f941/linq.js ]
code at JSfiddle: http://jsfiddle/Irfanmunir/gLXNw/2/
Share Improve this question asked Jan 8, 2013 at 18:05 irfan Munirirfan Munir 1513 silver badges17 bronze badges 3-
You're returning a string, which is "truthy" (evaluates to true). You probably want to actually evaluate your filter in the where callback:
.Where(function (x) { return x['A'] == 28; })
– Shmiddty Commented Jan 8, 2013 at 18:10 - It will not work as i am passing a variable field for where clause. The solution mention below works – irfan Munir Commented Jan 9, 2013 at 12:03
- Can you update the linq.js path. The path you have given is broken, So all the jsfiddle are not working. – Rajon Tanducar Commented Jan 25, 2022 at 11:59
2 Answers
Reset to default 3your filter is a string which always evaluates to true. put your filter inside a function:
var filter = function(x) { return x['A'] === 28 };
and use this:
.Where(filter)
see updated fiddle: http://jsfiddle/gLXNw/4/
You either have to pass a predicate function, or a string that represents such a function. You're passing a function, so linq.js doesn't expect another function/string.
For Linq.js, you have to use this syntax for a string:
var filter = "x => x['A']==28"; // also note the quotes surrounding A
You then pass this function string to .Where
:
.Where(filter)
You can of course also inline this:
.Where("x => x['A']==28")
http://jsfiddle/gLXNw/3/