我有四个输入数组,它们都对应于一个输出数组,如下所示:
var new = [{input: [.84, .54, 1, .65], output: [1]}, {input: [.01, .09, .26, .84], output: [0]}, ... {input: [.43, .99, .414, .46], output: [1]}];
arr1,arr2,arr3和arr4是输入数组,输出是输出数组。我需要能够格式化这些数据,以便将输入数组的每个对应元素放入一个数组中,并将输出数组中的相应元素放入另一个数组中,如下所示:
wait_event_interruptible()
所有数组的长度相同(以上仅为示例)。我正在使用Node.js;所有的帮助将不胜感激!
答案 0 :(得分:0)
可能有一些更高效的方法,但我只是使用一个简单的for循环。
var arr1 = [.84, .01, .95, 1, .22, .453, .194, .43];
var arr2 = [.54, .09, .10, 0, .76, .12, .41, .99];
var arr3 = [1, .26, .90, .65, .88, .33, .854, .414];
var arr4 = [.65, .84, .184, .81, .04, .09, 0, .46];
var output = [1, 0, 0, 1, 1, 1, 0, 1];
var new = [];
for (var i = 0; i < output.length; i++) {
var input = [arr1[i], arr2[i], arr3[i], arr4[i]];
var output = [output[i]];
new.push({input: input, output: output});
}
这会为您提供所需的输出。
答案 1 :(得分:0)
这是使用某些功能编程的最佳时机。我更喜欢lodash.js。
var arr1 = [.84, .01, .95, 1, .22, .453, .194, .43];
var arr2 = [.54, .09, .10, 0, .76, .12, .41, .99];
var arr3 = [1, .26, .90, .65, .88, .33, .854, .414];
var arr4 = [.65, .84, .184, .81, .04, .09, 0, .46];
var output = [1, 0, 0, 1, 1, 1, 0, 1];
_(_.zip(arr1, arr2, arr3, arr4)).zip(output);
请查看the documentation获取_.zip,因为它有更好的解释和更多示例。
答案 2 :(得分:0)
比下面更清洁一点的实现,使用了一点FP但不需要lodash。 :)
var arr1 = [.84, .01, .95, 1, .22, .453, .194, .43];
var arr2 = [.54, .09, .10, 0, .76, .12, .41, .99];
var arr3 = [1, .26, .90, .65, .88, .33, .854, .414];
var arr4 = [.65, .84, .184, .81, .04, .09, 0, .46];
var output = [1, 0, 0, 1, 1, 1, 0, 1];
var transformed = output.map(function(v, i) {
return {
input: [arr1[i], arr2[i], arr3[i], arr4[i]],
output: [v]
};
});
答案 3 :(得分:0)
var resultsArray = []
for(var i =0; i < output.length; i++){
var currentRound = {input:[ arr1[i], arr2[i], arr3[i], arr4[i]], output: output[i]}
resultsArray.push(currentRound)
}
应该做你想做的事
编辑:超级晚会。