【问题标题】:how to create 2D using two different array or single array如何使用两个不同的数组或单个数组创建 2D
【发布时间】:2022-01-19 13:48:45
【问题描述】:

我想要这样的输出:

items = [['red', 'blue', 'green'], ['small', 'medium', 'large']]

我有一个具有值的数组项:

items = ['red', 'blue', 'green','small', 'medium', 'large']

【问题讨论】:

  • array.flat() 结束

标签: javascript jquery arrays multidimensional-array jagged-arrays


【解决方案1】:

如果变异不是问题,那么您可以删除初始数组,直到将它们 3 3 移动到新数组中。

const items = ['red', 'blue', 'green', 'small', 'medium', 'large'];

let itemList = [];

while (items.length > 0) {
    itemList.push(items.splice(0, 3))
}

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多