This article describes the JS-implemented array full permutation output algorithm. Share it for your reference. The specific analysis is as follows:
This js code performs full array output, improving some old code
Take any m (m≤n) elements from n different elements and arrange them in a certain order, which is called taking out an arrangement of m elements from n different elements. When m=n, all arrangements are called full arrangement.
function permute(input) { var permArr = [], usedChars = []; function main(input){ var i, ch; for (i = 0; i < input.length; i++) { ch = input.splice(i, 1)[0]; usedChars.push(ch); if (input.length == 0) { permArr.push(usedChars.slice()); } main(input); input.splice(i, 0, ch); usedChars.pop(); } return permArr } return main(input);};console.log(permute([5, 3, 7, 1]));I hope this article will be helpful to everyone's JavaScript programming.