如何使用 linqjs 获取具有特定 id 的书名

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

我有一个 JavaScript 数组,里面有 objects,其中包含一个 id,书名 usw,我需要搜索它并使用 linqjs 获取具有给定 id 的书名。

示例:

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

<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>

我的尝试:

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

根据 documentation,您必须先调用 JSLINQ(array)

因此,以您的示例为例。这应该工作。另外,我将你的数组转换为对象

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 

你可以用普通的 JavaScript 非常简单地做到这一点。这是代码,其中 returns 一组按 id 过滤的书籍数组。

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]);
var id = '211-44-2314';
var result = books.filter(function(b){ return b[0]==id; });
console.log(result);