简体   繁体   English

Lodash 等价于 C# LINQ GroupBy 和 TypeScript 中的 Select

[英]Lodash Equivalent to C# LINQ GroupBy and Select in TypeScript

I'm trying to find a way to do the equivalent of .GroupBy(key => key.IdShopItem).Select(item => item.Single());我试图找到一种方法来做等效的.GroupBy(key => key.IdShopItem).Select(item => item.Single()); in typescript.在打字稿中。

I've tried this LinQ for TypeScript https://github.com/kutyel/linq.ts but it seems there's a bug and also it does not support this so far.我已经LinQ for TypeScript https://github.com/kutyel/linq.ts尝试了这个LinQ for TypeScript但似乎有一个错误,而且到目前为止它也不支持这个。

Also I've tried with lodash https://lodash.com/docs/4.17.10#groupBy , but I had no success.我也试过lodash https://lodash.com/docs/4.17.10#groupBy ,但我没有成功。

How can I achieve this please?我怎样才能做到这一点? Already spend a couple of hours on this...已经花了几个小时在这...

Disclaimer: I wrote this library免责声明:我写了这个库

You could use the javascript loq library to achieve this.您可以使用 javascript loq库来实现这一点。 Here's a snippet to get you on your way:这里有一个片段可以让你上路:

 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>

...or with requirejs ...或使用 requirejs

 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>

It's a relatively complete reimplementation of LINQ-to-objects on iterables (objects that implement Symbol.iterator ) with a few extra goodies (such as distinctBy ).它是在可迭代对象(实现Symbol.iterator的对象)上相对完整的 LINQ-to-objects 重新实现,并带有一些额外的好处(例如distinctBy )。 Take a look at the tests to see other uses.查看测试以了解其他用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM