Search code examples
javascriptjqueryjslinq

How do I get the book title with a specific id using linqjs


I have a JavaScript array with objects in it which contain an id, book title usw, I need to search it and get the title with a given id preferable with linqjs.

Example:

"213-46-8915" : id
"The Busy Executive's Database Guid" : Title

<script>
   var books = [];
   books.push(["213-46-8915" , "The Busy Executive's Database Guid",19.9900,2]);
   books.push(["211-44-2314" , "The Busy Executive's Database Guid",14.4100,5]);
</script>

My try:

var filtered = Enumerable
                .From(books)
                .Where(function (x) {return x.contains(id);})
                .Select(function (x) {return x;})
                .ToArray()

Solution

  • According to the documentation, you have to call JSLINQ(array) first.

    So, for your example. This should work. Also, I converted your array to an object

    var books = [];
    books.push({
        id: "213-46-8915",
        title: "The Busy Executive's Database Guid",
        price: 19.9900,
        quantity: 2
    });
    
    var filtered = JSLINQ(books)
                   .Where(function(item) { return item.id == "213-46-8915"; })
                   .Select(function(item) { return item; });
    
    console.log(filtered.id); //213-46-8915