O
O
ojiekcahdp2021-01-22 00:32:12
JavaScript
ojiekcahdp, 2021-01-22 00:32:12

How to make a random selection of an object from a json file?

In general, there is a users.json file, it stores the data of each user, looks something like this:

[ 
{
"id": 123123,
"status": "wow" 
},
{
"id": 124124,
"status": "yes" 
}...
]

How to select a random "id"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-01-22
@ojiekcahdp

const users = require('users.json');

const indx = Math.round(Math.random() * users.length);

const user = users[indx];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question