2

我有一个 JavaScript 数组,其中包含一个对象,其中包含一个 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()
4

2 回答 2

0

根据文档,您必须先致电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 
于 2016-09-06T15:07:29.997 回答
0

您可以非常简单地使用纯 JavaScript 来完成。这是返回由 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);

于 2016-09-06T15:33:28.460 回答