How to set all array elements to a specific value in TypeScript

2 Answers

0 votes
const arr = new Array(3).fill('typescript');
 
console.log(arr);
   
   
   
   
/*
run:
   
["typescript", "typescript", "typescript"] 
   
*/

 



answered May 13, 2022 by avibootz
0 votes
let arr = [4, 1, 2, 8];
 
arr = new Array(arr.length).fill(55);
 
console.log(arr);
   
   
   
   
/*
run:
   
[55, 55, 55, 55] 
   
*/

 



answered May 13, 2022 by avibootz
...