遍历不规则行长度的矩阵

问题描述 投票:0回答:1

我有一个矩阵,其中行的长度不必相同:

const notes = [
    [ 'do5', 'mi5' ],
    [ 'mi6', 'so6', 'ti6', 're7' ],
    [ 'so7', 'ti7', 're8', 'fa8' ],
    [ 'la3', 'do4', 'mi4' ],
    [ 'fa2', 'la2' ],
    [ 're2' ],
    [ 'ti1', 're2', 'fa2' ]
];

我如何遍历此数组?

javascript multidimensional-array dimensions
1个回答
0
投票

您可以使用最近的.flat()功能。考虑到您的矩阵称为notes

for (const note of notes.flat()) {
  console.info(note)
}
© www.soinside.com 2019 - 2024. All rights reserved.