我有一个像这样的数组
const input_array= [
["black", "blue"],
["large", "medium"],
["a", "b", "c"]
//... is it dynamic can be added many rows
];
我该如何得到一个像这样的数组:
const finallist = [
["black", "large", "a"],
["black", "large", "b"],
["black", "large", "c"],
["black", "medium", "a"],
["black", "medium", "b"],
["black", "medium", "c"],
["blue", "large", "a"],
["blue", "large", "b"],
["blue", "large", "c"],
["blue", "medium", "a"],
["blue", "medium", "b"],
["blue", "medium", "c"],
]
请记住input_array是动态的
请告诉我如何做到这一点
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你可以像这样做:
const input_array = [ ["black", "blue"], ["large", "medium"], ["a", "b", "c"] ] const getCompinations = array => array.reduce((v, b) => v.reduce((r, g) => [...r, ...b.map(w => [].concat(g, w))], []) ) console.log(getCompinations(input_array))看一下这个,可能会有帮助:
const input_array = [ ["black", "blue"], ["large", "medium"], ["a", "b", "c"] //... 是否可以动态添加多行 ]; const mmc = input_array.reduce((e, r) => e * r.length, 1); const finallist = input_array.map((x,i)=>({index:i,arr:x})).reduce((e, r) => { for (var u = 0; u e[u].includes(r)) || e[u].length小心! 在大规模矩阵中可能会导致浏览器崩溃。