Under the Hood
Last week, I have covered an article on JSON-Server, how it uses lowdb and create sample API with all the required HTTP methods. If you people have any time used JSON-Placeholder API then for your information the JSON-placeholder is developed using
Link to JSON-Server: https://medium.com/p/4525def1aea5?source=post_stats_page-------------------------------------
While writing a blog about JSON npm, I was wondering that
The 2/3 most required methods you should know before creating a simple function.
- Math — This class has several important methods such as random and floor these methods create random fraction number and round them respectively.
- Array- This constructor helps to construct an array object for you
The idea is to first create an array, followed by creating a single user and using for loop we will fill the array with a single user object and this will run until the length of our array. Every single user will have an id, name and age as key-value pair. An id is a number that is in increasing order, the name is a random string which we will generate using the
random method and the age is again a number ranges from 1 to 100 so we will use
random and floor method to get the random age.
See these small methods are sometimes essentials in terms of the interview, so I will recommend trying on your own.
we have 3 functions here as follows-
- getRandomAge — This function creates a random age using a random method and age ranges from 1–100
- createUser — This function creates a single user by accepting id, and generating random age using
getRandomAgemethod and random name.
- createUsers — This function basically runs the for loop till the length of the array and create a new user every time using the
createUsermethod and pushed that user in the user's array.
After running the code, we will get an array of 10 users with random name and age and increasing order id. In this way, we have created a sample JSON data of 10 users. We can make it up to 100 also by just increasing the length of our
Until, next time. Have a good day,People.