Advertisements
I need to resize arrays by adding empty new columns and empty new rows.
Ex
newRows = 2
newCols = 3
(1s are just placeholder for any data)
arr= [
[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
];
To
arr= [
[1,1,1,1,"","",""],
[1,1,1,1,"","",""],
[1,1,1,1,"","",""],
["","","","","","",""],
["","","","","","",""],
];
I can get the new empty cols but not the new empty rows
function resizeArr() {
let arr= [
[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
];
let newCols =3
newRows = 2;
//Add new empty cols
arr.forEach((row) => {
row[arr.length+newCols]="";
});
console.log(arr)
}
How to add new empty rows ?
Thanks
>Solution :
let arr = [
[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
];
let newCols = 3, newRows = 2
arr.forEach(i=>i.push(...Array(newCols).fill('')))
for(let i=0; i<newRows; i++) arr.push(Array(arr[0].length).fill(''))
console.log(arr)