where function of Linq.js on Json data not working

irfan Munir picture irfan Munir · Jan 8, 2013 · Viewed 7.7k times · Source

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 complete 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.com/gist/1175460/fb7404d46cab20e31601740ab8b35d99a584f941/linq.js ]

code at JSfiddle: http://jsfiddle.net/Irfanmunir/gLXNw/2/

Answer

hereandnow78 picture hereandnow78 · Jan 8, 2013

your 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.net/gLXNw/4/