How to create an array of json containing 100 json objects in javascript using for loop with properties id and name. Use Math.random() to create random ids. The random number should be between 1-100. E.g. [{id:1,"name":"lorem"},{id:2,"name":"ipsum"}....]
-
1Post code that you have tried otherwise you will more than likely get no help.Ryan Wilson– Ryan Wilson2018-03-15 14:31:50 +00:00Commented Mar 15, 2018 at 14:31
-
Actually i am not getting idea where to start from. Please help me.Klaus– Klaus2018-03-15 14:39:53 +00:00Commented Mar 15, 2018 at 14:39
-
Read these: for loop, random number, random stringgforce301– gforce3012018-03-15 14:43:25 +00:00Commented Mar 15, 2018 at 14:43
Add a comment
|
1 Answer
var out = new Array(100).fill(1).map(function(val, index){
return {
"name": "Name number: " + index,
"id": Math.floor((Math.random() * 100) + 1)
}
});
console.log(out);
~or~
var out = [];
for(var i = 0; i < 100; i++){
out.push({
"name": "Name number: " + i,
"id": Math.floor((Math.random() * 100) + 1)
});
};
console.log(out);