简体   繁体   English

Typescript 生成数组中n个元素的所有组合

[英]Typescript generate all combinations of n elements in array

I have an array of n elements and I want to generate all combinations of k elements of that array.我有一个包含 n 个元素的数组,我想生成该数组的 k 个元素的所有组合。

For example: ['a', 'b', 'c'] .例如: ['a', 'b', 'c'] If k is 1 then I would have [['a'],['b'], ['c']] .如果 k 为 1,那么我将有[['a'],['b'], ['c']]

If k is 2 then [['a', 'b'], ['a', 'c'], ['b', 'c']]如果 k 为 2,则[['a', 'b'], ['a', 'c'], ['b', 'c']]

if k is 3 then [['a', 'b', 'c']]如果 k 为 3,则[['a', 'b', 'c']]

Thank you in advance先感谢您

I found an answer using this library https://www.npmjs.com/package/combinations which does exactly what I want.我使用这个库https://www.npmjs.com/package/combinations找到了答案,这正是我想要的。

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

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