Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用reduce(javascript)在数组中添加数对_Javascript - Fatal编程技术网

使用reduce(javascript)在数组中添加数对

使用reduce(javascript)在数组中添加数对,javascript,Javascript,给定一个2D数组,我想将前一个内部数组的最后一个数添加到下一个内部数组的第一个数 我设法做到了以下几点: var output= [[1,2,3],[4,5,6],[7,8,9],[1,2,3]] //this becomes... output = [3,4,6,7,9,1] (edited typo) 现在,我想添加对以返回此数组: output = [9, 11, 10] 到目前为止,这就是我所拥有的,它返回[3,6,4,7,9,1]。我想看看reduce如何用于此,但也对for循环如

给定一个2D数组,我想将前一个内部数组的最后一个数添加到下一个内部数组的第一个数

我设法做到了以下几点:

var output= [[1,2,3],[4,5,6],[7,8,9],[1,2,3]] //this becomes...
output = [3,4,6,7,9,1] (edited typo)
现在,我想添加对以返回此数组:

output = [9, 11, 10]
到目前为止,这就是我所拥有的,它返回[3,6,4,7,9,1]。我想看看reduce如何用于此,但也对for循环如何完成相同的任务感兴趣

var output= [[1,2,3],[4,5,6],[7,8,9],[1,2,3]]
output = output
    .reduce((newArr,currArr)=>{
    newArr.push(currArr[0],currArr[currArr.length-1]) //[1,3,4,6,7,9]
    return newArr
  },[])
    output.shift()
    output.pop()
return output

可以使用reduce的索引参数

let output=[[1,2,3]、[4,5,6]、[7,8,9]、[1,2,3];
输出=输出
.reduce((newArr、currar、i、origar)=>{
如果(i>0){
设prevArr=origArr[i-1];
newArr.push(currArr[0]+prevArr[prevArr.length-1]);
}        
返回纽瓦尔
}, [])

console.log(输出)
可以使用reduce的索引参数

let output=[[1,2,3]、[4,5,6]、[7,8,9]、[1,2,3];
输出=输出
.reduce((newArr、currar、i、origar)=>{
如果(i>0){
设prevArr=origArr[i-1];
newArr.push(currArr[0]+prevArr[prevArr.length-1]);
}        
返回纽瓦尔
}, [])

console.log(输出)
不清楚输入数组的最后一个元素应该发生什么?您可以使用
for..of
循环,
Array.prototype.entries()
对数组的特定索引值求和

let输出=[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2, 3]
];
设res=Array.from({
长度:output.length-1
});
for(让output.entries()的[key,value]{
如果(键console.log(res)
不清楚输入数组的最后一个元素应该发生什么?您可以使用
for..of
循环,
Array.prototype.entries()
对数组的特定索引值求和

let输出=[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2, 3]
];
设res=Array.from({
长度:output.length-1
});
for(让output.entries()的[key,value]{
如果(键log(res)
您可以使用reduce执行类似的操作

var输入=[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2, 3]
];
让输出=[];
input.reduce((prevArr,currentary)=>{
if(prevArr){
push(prevArr[prevArr.length-1]+currentArray[0]);
}
返回电流阵列;
});

控制台日志(输出)您可以使用reduce执行类似的操作

var输入=[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2, 3]
];
让输出=[];
input.reduce((prevArr,currentary)=>{
if(prevArr){
push(prevArr[prevArr.length-1]+currentArray[0]);
}
返回电流阵列;
});

控制台日志(输出)
根据您对问题的描述,您肯定希望输出是
[7,13,10]
(我的工作:
[3+4,6+7,9+1]
)?或者我误解了?事实上,获取
[3,6,4,7,9,1]
的逻辑很奇怪,并且不像你在第一段中描述的那样。根据你对问题的描述,你肯定希望输出是
[7,13,10]
(我的工作:
[3+4,6+7,9+1]
)?或者我误解了?事实上,获取
[3,6,4,7,9,1]
的逻辑很奇怪,不像你在第一段中描述的那样。