create an array with ones in javascript

You can create an array with ones using the Array.fill() method in JavaScript. Here's an example:

index.tsx
// create an array with 5 ones
const onesArray = Array(5).fill(1);
console.log(onesArray); // [1, 1, 1, 1, 1]
110 chars
4 lines

In this example, we first create a new array with a length of 5 by passing the value 5 to the Array() constructor. We then call the fill() method on the array and pass in the value 1. This sets all elements in the array to 1, resulting in an array containing 5 ones.

gistlibby LogSnag