以某种方式(从右到左)合并、扁平化和连接 javascript 中的多个多维数组的任何简单方法?
Any easy way to merge, flattern and concat multiple multi-dimensional arrays in javascript in a certain way (right to left)?
我需要一种简单的方法来以某种方式(从右到左)javascript 合并、扁平化和连接多个多维数组
# Exemple
[['.class1', '.class2'], ['.class3', ['.class4', '.class5', ...], ['.class6'], ...]]
# OR
[['.class1', '.class2'], ['.class3', ['.class4', '.class5', ...]], ['.class6'], ...]
# Would become
['.class1.class3.class4.class6', '.class1.class3.class5.class6', '.class2.class3.class4.class6', '.class2.class3.class5.class6', ...]
我从 underscore.js 库中找到了 reduceRight 函数,但我不确定如何轻松实现它,因为它需要递归完成。
对于 Array#forEach
.
的可变长度部分,您可以使用迭代和递归方法
此版本现在可以使用嵌套数组并在高级中将其扁平化。
function combine(array) {
function c(part, index) {
var temp = array[index];
if (Array.isArray(temp) && temp.some(function (a) { return Array.isArray(a); })) {
temp = combine(array[index].map(function (a) { return Array.isArray(a) ? a : [a]; }));
}
temp.forEach(function (a) {
var p = part.concat(a);
if (p.length === array.length) {
r.push(p.join(''));
return;
}
c(p, index + 1);
});
}
var r = [];
c([], 0);
return r;
}
var array = [
['.class1', '.class2'],
['.class3',
['.class4', '.class5'],
['.class6']
]
],
result = combine(array);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果您愿意在多维数组的每个元素上使用 lodash, which is a better version of underscore (imo), this can be expressed fairly simply using flattenDeep
(https://lodash.com/docs/4.17.4#flattenDeep):
function combine(list) {
const arrays = list.map(_.flattenDeep);
return arrays.map((a) => a.join('');
}
var data = [['.class1', '.class2'], ['.class3', ['.class4', '.class5'], ['.class6']]];
function cartesian(a, b) { // return the cartesian product of the two arrays a and b
if(!a.length) return b; // if a is empty then the result of the product is b
if(!b.length) return a; // if b is empty then the product is a
return a.reduce((res, ae) => (b.forEach(be => res.push(ae + be)), res), []); // product of non-empty a and non-empty b
}
function combos(arr) { // take an array arr and return the combinations out from its nested arrays
if(arr.every(e => !Array.isArray(e))) return arr; // if the array doesn't contain any nested arrays then return it as it is
return arr.reduce((acc, e) => { // otherwise return the cartesian product of all its elements
e = Array.isArray(e)? combos(e): [e]; // if the current element is an array, then get its combos, otherwise, wrap it in an array
return cartesian(acc, e); // get the cartesian product of previous elements and the combos of this element e
}, []);
}
console.log(combos(data));
我需要一种简单的方法来以某种方式(从右到左)javascript 合并、扁平化和连接多个多维数组
# Exemple
[['.class1', '.class2'], ['.class3', ['.class4', '.class5', ...], ['.class6'], ...]]
# OR
[['.class1', '.class2'], ['.class3', ['.class4', '.class5', ...]], ['.class6'], ...]
# Would become
['.class1.class3.class4.class6', '.class1.class3.class5.class6', '.class2.class3.class4.class6', '.class2.class3.class5.class6', ...]
我从 underscore.js 库中找到了 reduceRight 函数,但我不确定如何轻松实现它,因为它需要递归完成。
对于 Array#forEach
.
此版本现在可以使用嵌套数组并在高级中将其扁平化。
function combine(array) {
function c(part, index) {
var temp = array[index];
if (Array.isArray(temp) && temp.some(function (a) { return Array.isArray(a); })) {
temp = combine(array[index].map(function (a) { return Array.isArray(a) ? a : [a]; }));
}
temp.forEach(function (a) {
var p = part.concat(a);
if (p.length === array.length) {
r.push(p.join(''));
return;
}
c(p, index + 1);
});
}
var r = [];
c([], 0);
return r;
}
var array = [
['.class1', '.class2'],
['.class3',
['.class4', '.class5'],
['.class6']
]
],
result = combine(array);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果您愿意在多维数组的每个元素上使用 lodash, which is a better version of underscore (imo), this can be expressed fairly simply using flattenDeep
(https://lodash.com/docs/4.17.4#flattenDeep):
function combine(list) {
const arrays = list.map(_.flattenDeep);
return arrays.map((a) => a.join('');
}
var data = [['.class1', '.class2'], ['.class3', ['.class4', '.class5'], ['.class6']]];
function cartesian(a, b) { // return the cartesian product of the two arrays a and b
if(!a.length) return b; // if a is empty then the result of the product is b
if(!b.length) return a; // if b is empty then the product is a
return a.reduce((res, ae) => (b.forEach(be => res.push(ae + be)), res), []); // product of non-empty a and non-empty b
}
function combos(arr) { // take an array arr and return the combinations out from its nested arrays
if(arr.every(e => !Array.isArray(e))) return arr; // if the array doesn't contain any nested arrays then return it as it is
return arr.reduce((acc, e) => { // otherwise return the cartesian product of all its elements
e = Array.isArray(e)? combos(e): [e]; // if the current element is an array, then get its combos, otherwise, wrap it in an array
return cartesian(acc, e); // get the cartesian product of previous elements and the combos of this element e
}, []);
}
console.log(combos(data));