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

时间:2016-09-06 14:54:58

标签: javascript jquery jslinq

我有一个JavaScript数组,其中包含一个id,书名为usw,我需要搜索它并获得带有linqjs的给定id的标题。

示例:

" 213-46-8915" :id
"繁忙的执行官数据库指导" :标题

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);