计算 1 个数组中元素的笛卡尔乘积
Compute cartesian product of elements in 1 array
我想计算数组单个元素内的笛卡尔积。一次只能有 2 个值。
所以如果这个我的数组是:
[["cat dog mouse"], ["blue red green"]]
期望值为:
- 猫, 狗
- 猫, 老鼠
- 狗, 老鼠
- 蓝色,红色
- 蓝色,绿色
- 红色、绿色
这是我的错误方法:
var arr = [["cat dog mouse"], ["blue red green"], ["apple orange banana"]];
for (i = 0; i < arr.length; i++) {
for(j = 0; j < arr[i].length; j++){
if(j >= arr[i].length){
console.log(arr[i][j].split( " ") + " " + arr[i][0])
}else{
console.log(arr[i][j].split( " ") + " " + arr[i][j+1])
}
}
}
它给了我
- 猫,狗,老鼠未定义
- 蓝色,红色,绿色未定义
您可以使用
split()
和for
循环来做这样的事情
var arr = [
["cat dog mouse"],
["blue red green"],
["apple orange banana"]
];
// iterate the array
for (i = 0; i < arr.length; i++) {
// split the string
var inArr = arr[i][0].split(' ');
// iterate the splitted string array
for (j = 0; j < inArr.length - 1; j++) {
// iterate string next to current string
for (k = j + 1; k < inArr.length; k++)
// generate the output
console.log(inArr[j] + ', ' + inArr[k]);
}
}
[["cat dog mouse"], ["blue red green"]].forEach(function (item) {
item.forEach(function (value) {
var arr = value.split(' ');
var hash = {};
arr.forEach(function (firstItem) {
arr.forEach(function (secondItem) {
if (firstItem !== secondItem && !hash[firstItem + secondItem] && !hash[secondItem + firstItem]) {
console.log(firstItem + ', ' + secondItem);
hash[firstItem+secondItem] = true;
}
});
})
});
});
你很接近,一些修改的代码
var arr = [["cat dog mouse"], ["blue red green"], ["apple orange banana"]];
for (i = 0; i < arr.length; i++) {
var items = arr[i][0].split(" ")
for (j = 0; j < items.length - 1; j++) {
for (k = j+1; k < items.length; k++) {
console.log(items[j], items[k])
}
}
}
结果:
cat dog
cat mouse
dog mouse
blue red
blue green
red green
apple orange
apple banana
orange banana
你可以
用函数式的方式做到这一点,只需使用 .map() 和 .forEach() 方法:
var arr = [["cat dog mouse"], ["blue red green"], ["apple orange banana"]];
function print(item){
//If there is one last item we stop the process
if (item.length <= 1) return;
else {
//Retrieve head of our stack and shift the first item
const head = item.shift();
item.forEach((elm) => console.log(head + ',' + elm));
// call our recursive function with the new array
return print(item);
}
}
function combine(arr){
//Return array splited by using .map and .split operations, then iterate by using forEach and call our
// recursive function
arr
.map((array) => array[0].split(' '))
.forEach((value) => print(value));
}
combine(arr);
你可以检查普伦克
拆分为部分解决方案的解决方案,一个用于数组,一个用于组合,不重复。
function combine(array, length) {
function c(l, r) {
var ll = l.slice();
if (r.length === length) {
result.push(r);
return;
}
while (ll.length) {
c(ll, r.concat(ll.shift()));
}
}
var result = [];
c(array, []);
return result;
}
function get(array) {
return array.map(function (a) {
return combine(a[0].split(' '), 2);
});
}
var result = get([["cat dog mouse"], ["blue red green"], ["apple orange banana"]]);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
相关文章:
- 如何更改拉斐尔元素图片
- 选项卡式元素上的动画CSS转换
- 如何在鼠标悬停时显示带有拉斐尔元素(圆、线)的上下文菜单
- 多维数组的笛卡尔乘积
- 没有重复的笛卡尔乘积
- 笛卡尔乘积的反转
- 计算 1 个数组中元素的笛卡尔乘积
- 引导选项卡标题元素中的复杂 html
- 通过循环访问未知数量的数组来创建JavaScript对象的笛卡尔乘积(powerset?)
- 2 个数组的笛卡尔积
- javascript中对象的笛卡尔乘积
- 如何将两个数组组合为笛卡尔积
- 如何使用科学记数法使(笛卡尔)图表轴按比例缩放
- 如何发送拉斐尔元素.toBack()
- [Javascript]获取所有数组组合(笛卡尔乘积)
- 如何在javascript中实现不同长度n数组的笛卡尔乘积
- 在javascript中获取最近的笛卡尔轴对齐矢量
- 拉斐尔元素没有'当传递给循环中的函数时,t变换
- 极坐标到笛卡尔坐标的函数没有输出正确的数据
- 笛卡尔坐标与球坐标的转换