【问题标题】:nested forEach unexpectedly overwriting [duplicate]嵌套的forEach意外覆盖[重复]
【发布时间】:2018-04-15 08:58:30
【问题描述】:

我正在尝试制作一个将数组的所有“水平值”转换为“垂直值”的函数,以便每个 array[i][j] 变成 newarray[j][i]

[ [ '00', '10', '20' ], 
  [ '01', '11', '21' ], 
  [ '02', '12', '22' ] ];

应该变成

[ [ '00', '01', '02' ],
  [ '10', '11', '12' ],
  [ '20', '21', '22' ] ]

这是我目前拥有的脚本:

let board = 
[ [ '00', '10', '20' ], 
[ '01', '11', '21' ], 
[ '02', '12', '22' ] ];

let col;

const horizToVert= (arg)=>{
  const init = Array(arg.length).fill(Array(arg[0].length).fill(''));
  arg.forEach((value, index) => value.forEach((value2, index2) => {
    init[index2][index]=value2; console.log(init);
  })); 
  return init;
}

col = horizToVert(board);

但是由于某种原因,输出对我来说毫无意义:

[ [ '00', '', '' ], [ '00', '', '' ], [ '00', '', '' ] ]
[ [ '10', '', '' ], [ '10', '', '' ], [ '10', '', '' ] ]
[ [ '20', '', '' ], [ '20', '', '' ], [ '20', '', '' ] ]
[ [ '20', '01', '' ], [ '20', '01', '' ], [ '20', '01', '' ] ]
[ [ '20', '11', '' ], [ '20', '11', '' ], [ '20', '11', '' ] ]
[ [ '20', '21', '' ], [ '20', '21', '' ], [ '20', '21', '' ] ]
[ [ '20', '21', '02' ],[ '20', '21', '02' ],[ '20', '21', '02' ] ]
[ [ '20', '21', '12' ],[ '20', '21', '12' ],[ '20', '21', '12' ] ]
[ [ '20', '21', '22' ],[ '20', '21', '22' ],[ '20', '21', '22' ] ]
[Finished in 0.727s]

为什么将例如'00' 分配给所有col[i][0] 索引?

【问题讨论】:

标签: javascript arrays foreach


【解决方案1】:

因为Array.fill

fill() 方法用静态值填充数组中从开始索引到结束索引的所有元素。

获取一个静态值并用它填充数组。因此,您可以在 init 的每个元素中得到相同的填充数组。

const horizToVert = (arg) => {
    const init = Array(arg.length).fill(Array(arg[0].length).fill(''));

    init[1][2] = 'foo';

    //arg.forEach((value, index) => value.forEach((value2, index2) => {
    //    init[index2][index] = value2;
    //    console.log(init);
    //}));
    return init;
}

let board = [['00', '10', '20'], ['01', '11', '21'], ['02', '12', '22']];

let col = horizToVert(board);

console.log(JSON.stringify(col, 0, 4));
.as-console-wrapper { max-height: 100% !important; top: 0; }

要获得一个独立的填充数组,您可以使用Array.from 并使用映射值映射一个新数组。

var array = Array.from({ length: 3 }, _ => Array.from({ length: 3 }, _ => 4));

array[0][0] = 0;
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您可以为不存在的数组使用默认数组并将值分配给切换的索引。

var array = [['00', '10', '20'], ['01', '11', '21'], ['02', '12', '22']],
    result = array.reduce(
        (r, a, i) => (a.forEach((v, j) => (r[j] = r[j] || [])[i] = v), r),
        []
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 我知道这是一个实际可行的实现,但我想知道为什么会发生这种意外行为。
  • @MicksKetches,你里面只有一个数组。每个分配都转到所有三个内部数组,因为它们不是独立的。
  • 感谢扩展答案,我完全不知道 .fill() 的这种行为!
【解决方案2】:

你可以这样用。

let board = 
[ [ '00', '10', '20' ], 
[ '01', '11', '21' ], 
[ '02', '12', '22' ] ];


var transpond = board[0].map((col, i) => board.map(row => row[i]));

console.log(transpond);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多