Javascript从两个对象数组生成矩阵
尝试将无限多个对象数组转换为矩阵Javascript从两个对象数组生成矩阵,javascript,arrays,Javascript,Arrays,尝试将无限多个对象数组转换为矩阵 height: [1,3,4,5,6,7] weight: [23,30,40,50,90,100] 进入 基本上把所有可能的组合画成一个矩阵 我试着用下划线.js中的几个函数来解决这个问题 var firstOption = _.first( productOptionKeys ); $.each( productOptions[ firstOption ].split(","), functi
height: [1,3,4,5,6,7]
weight: [23,30,40,50,90,100]
进入
基本上把所有可能的组合画成一个矩阵
我试着用下划线.js中的几个函数来解决这个问题
var firstOption = _.first( productOptionKeys );
$.each( productOptions[ firstOption ].split(","), function(index, value){
var matrixRow = [];
var matricableOptionKeys = _.reject( productOptionKeys, function(option){ return (option == firstOption); } );
matrixRow.push( value );
$.each( matricableOptionKeys, function( index, value ){
var matricableOptions = productOptions[ value ].split(",");
var matricIndex = 0;
for( i=0 ; i<matricableOptions.length; i++ ){
matrixRow.push( matricableOptions[ matricIndex ] );
}
//matricIndex ++;
// $.each( productOptions[ value ].split(","), function(index, value){
// matrixRow.push( value );
// return false;
// });
});
console.log( matrixRow );
});
现有的解决方案只找到数组的排列,而不是对象。每个键的值将始终以逗号分隔,因此对象[key].split(“,”)将以数组形式返回值
如果可以连接参数来调用函数,我可以使用现有函数中的一个。当然,可以使用double来:
var result = [];
for(var i=0; i<height.length; i++)
for(var j=0; j<weight.length; j++)
result.push({ height: height[i], weight: weight[j]});
试试这个
var height = [1,3,4,5,6,7];
var weight = [23,30,40,50,90,100];
var res = [];
for(h = 0; h < height.length; h++) {
for(w = 0; w < weight.length; w++) {
res.push({h: height[h], w: weight[w]});
}
}
var高度=[1,3,4,5,6,7];
变量权重=[23,30,40,50,90100];
var-res=[];
对于(h=0;h
您必须展示一次尝试,您不应该只是要求他人为您编写代码您尝试过的代码在哪里?“尝试转换无限多的对象数组”。如果它是无限的,恐怕你不能在有限的时间内完成它。@Oriol不完全是无限的,我想我是想传达一个信息,一个人会指定更多dimensions@JuanMendes我不认为有必要展示我的尝试。但请理解,这不是我的代码论坛
var result = [];
for(var i=0; i<height.length; i++)
for(var j=0; j<weight.length; j++)
result.push({ height: height[i], weight: weight[j]});
var result = [];
height.forEach(function(h){
weight.forEach(function(w){
result.push({ height: h, weight: w});
});
});
var height = [1,3,4,5,6,7];
var weight = [23,30,40,50,90,100];
var res = [];
for(h = 0; h < height.length; h++) {
for(w = 0; w < weight.length; w++) {
res.push({h: height[h], w: weight[w]});
}
}