I have a matrix containing arrays of rows.
let matrix=[['hello'],['world']];
I’m duplicating rows.
matrix=matrix.map(x=>String(x).repeat(2)).map(x=>x.match(new RegExp??))
I want to get
[['hello','hello'],['world','world']]
I have a matrix containing arrays of rows.
let matrix=[['hello'],['world']];
I’m duplicating rows.
matrix=matrix.map(x=>String(x).repeat(2)).map(x=>x.match(new RegExp??))
I want to get
[['hello','hello'],['world','world']]
2
Answers
If you want to duplicate the arrays, you can use concat
Or given that you have an array of arrays and you want to get an N number of entries instead of 1:
Your code wouldn’t work. First there’s no
Array::repeat()
method, second there’s no any sense to useRegExp::match()
to duplicate an array, third you’re trying to assign to a const variable. Use simpler tools.Since you overwrite the original array you can just mutate it:
That would be definitely faster than overwriting with a new array: