javascript - 如何使用 match() 分割数组中的行?

标签 javascript arrays string-matching

我有一个包含行数组的矩阵。

let matrix=[['hello'],['world']];

我正在复制行。

matrix=matrix.map(x=>String(x).repeat(2)).map(x=>x.match(new RegExp??))

我想要得到

[['hello','hello'],['world','world']]

最佳答案

如果你想复制数组,可以使用concat

const matrix = [
  ['hello'],
  ['world']
];
const res = matrix.map(a => a.concat(a))
console.log(res);

或者假设您有一个数组数组,并且您想要获取 N 个条目而不是 1 个:

const matrix = [
  ['hello'],
  ['world']
];
const res = matrix.map(a => Array(4).fill(...a));
console.log(res);

关于javascript - 如何使用 match() 分割数组中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77005871/

相关文章:

javascript - 使用纯 Javascript 不循环选择和修改元素集合

c - 为什么此代码不会打印项目列表?我知道这与返回有关

c - 我需要帮助过滤 C 中的坏词吗?

javascript - HML5/JS 版本 "WordPress Audio Player?"

javascript - 将表单提交到弹出窗口?

Javascript多维数组错误

javascript - 从 JavaScript 数组中获取随机项

python - 在包含项目列表的列中查找共同值

Java 字符串与枚举的匹配

javascript - Node.js 中的 package.json 中未显示依赖项