0

I have a JSON array (javascript). I am using linq.js to filter/query the array and find a single element/first element. What is the correct syntax?

My data:

[{
    "Fname": "Chinmaya",
    "Lname": "Bhatta",
    "DOB": "/Date(328645800000)/",
    "PassportNumber": "",
    "Expirydate": "/Date(315513000000)/",
    "Mobilenum": "91-9740814702",
    "EmailID": "[email protected]",
    "IssueCountry": ""
},
    ...
]

My code:

var tbl =  $(this).closest('table');

var fname= $(tbl).find('[id*=txtFirstName]').val();

var lname= $(tbl).find('[id*=txtLastName]').val();

var filtered = Enumerable.From(fulllist)
                         .Select("$.Fname + ':'+ $.fname+")
                         .ToArray();

But it is throwing error. Can someone tell me what is the right syntax for querying based on Fname & Lname? BTW fulllist is the name of my array.

Thanks in advance.

1 Answer 1

2

jsFiddle

Just like with a SQL query, you have to specify your 'where' clause to filter the results from the entire dataset:

var filtered = Enumerable
            .From(data)
            .Where("$.Fname == '" + fname + "' && $.Lname == '" + lname + "'")
            .Select("$.Fname + ':' + $.Lname + ':' + $.DOB")
            .ToArray();

You can also use the explicit form of the Where syntax which accepts a callback function:

Where(function (x) { return x.Fname == fname && x.Lname == lname });

Edit

To have your filtered result set be an array of JS obects, remove the call to Select (jsFiddle 2):

var filtered = Enumerable
               .From(data)
               .Where("$.Fname == '" + fname + "' && $.Lname == '" + lname + "'")
               .ToArray();
Sign up to request clarification or add additional context in comments.

6 Comments

Hi nbrooks, thank you for quick response. It returns all the matching values. Actually I wanted only one row/one record only. Second, how to access the data of the single record in javascript code?
Hi nbrooks, this method, only returns the values, not the filed names. I wanted it like {"Fname": "Chinmaya", "Lname": "Bhatta"} format. But I am getting only the values. Pls help me out.
@santubangalore Do you want your result to be a string, or an object? See edit above, which returns the result as an object.
Hi nbrooks, thanks, my second problem is solved by this, but the first problem still remains. i.e. I wanted only one row/record, but I am getting multiple rows. How do I filter it to only one row? And access the data items from javascript code?
@santu My demo above is filtering the resultset to a single row, and is also logging the result (with JavaScript) to the console. If something isn't working, please edit your question to describe both the expected and actual results, and edit the jsfiddle (or make your own) to reproduce the problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.