[英]Lodash Equivalent to C# LINQ GroupBy and Select in TypeScript
我試圖找到一種方法來做等效的.GroupBy(key => key.IdShopItem).Select(item => item.Single());
在打字稿中。
我已經LinQ for TypeScript
https://github.com/kutyel/linq.ts嘗試了這個LinQ for TypeScript
但似乎有一個錯誤,而且到目前為止它也不支持這個。
我也試過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>
...或使用 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>
它是在可迭代對象(實現Symbol.iterator
的對象)上相對完整的 LINQ-to-objects 重新實現,並帶有一些額外的好處(例如distinctBy
)。 查看測試以了解其他用途。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.