Create an array of n size

Have you ever needed to created an array of n size just to test the UI in frontend. Well most of the time while implemented a new Design where we need to show rendering of a component multiple time we manually creates an array but what if we need an array of 1000 items. That will be difficult to create manually. In this tip article we will gonna create an array of n size without lodash.

Solution

The solution is pretty simple. We will gonna use Array.from method to create an array of length n.

Array.from({ length: 8 });
// [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]

You can use this method to test your components.

Array.from({ length: 8 }).map((a, index) => (
            <ReactList/>
          ))

There is a lot of uses of array.from method.

Tip 1: Check if Object is Empty in JavaScript without lodash


2 Comments

How to use JSX in markdown documents - Blog React · July 29, 2020 at 3:23 am

[…] Tip 2: Create an array of n size without lodash […]

How to use JSX in markdown documents - Carstly News · August 1, 2020 at 3:42 pm

[…] Tip 2: Create an array of n size without lodash […]

Comments are closed.