代码
/**
* 获取 words 成员的所有 组合 方式
* @param {(string | number)[]} words
* @return {(string|number)[][]}
*/
function combine (words) {
const list = []
words.forEach((word, idx) => {
const restWords = [...words.slice(0, idx), ...words.slice(idx + 1)]
if (!restWords.length) { // 最后一位了
list.push([word])
} else {
const groups = combine(restWords)
groups.forEach(group => {
list.push([word, ...group])
})
}
})
return list
}
输出
combine(['A', 'B', 'C'])
[
[ 'A', 'B', 'C' ],
[ 'A', 'C', 'B' ],
[ 'B', 'A', 'C' ],
[ 'B', 'C', 'A' ],
[ 'C', 'A', 'B' ],
[ 'C', 'B', 'A' ]
]
标签:...,const,数组,元素,list,restWords,获取,combine,words
From: https://www.cnblogs.com/taohuaya/p/18004410