我正在尝试找到一种方法来执行打字稿中的.GroupBy(key => key.IdShopItem).Select(item => item.Single());
。
我已经尝试过这个LinQ for TypeScript
https://github.com/kutyel/linq.ts 但似乎有一个错误,而且到目前为止它不支持这个。
我也尝试过lodash
https://lodash.com/docs/4.17.10#groupBy,但没有成功。
请问我怎样才能做到这一点?已经花了几个小时在这上面...
免责声明:我写了这个库
您可以使用 javascript loq 库来实现这一点。这里有一个片段可以让你上路:
var list = [
{name:"dog",type:"animal"},
{name:"cat",type:"animal"},
{name:"snake",type:"animal"},
{name:"table",type:"furniture"},
{name:"chair",type:"furniture"},
];
var filtered = loq(list).distinctBy(item => item.type);
console.log(filtered.toArray());
<script src="https://cdn.rawgit.com/biggyspender/loq/67db7014/lib/loq.min.js"></script>
。或与要求JS
requirejs.config({
paths: {
loq: 'https://cdn.rawgit.com/biggyspender/loq/67db7014/lib/loq.min'
}
});
require(['loq'], function(loq){
var list = [
{name:"dog",type:"animal"},
{name:"cat",type:"animal"},
{name:"snake",type:"animal"},
{name:"table",type:"furniture"},
{name:"chair",type:"furniture"},
];
var filtered = loq(list).distinctBy(item => item.type);
console.log(filtered.toArray());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.js"></script>
这是对可迭代对象(实现Symbol.iterator
的对象(上的 LINQ to 对象的相对完整的重新实现,并带有一些额外的好处(例如distinctBy
(。查看测试以了解其他用途。