-3

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"}....]

3
  • 1
    Post code that you have tried otherwise you will more than likely get no help. Commented Mar 15, 2018 at 14:31
  • Actually i am not getting idea where to start from. Please help me. Commented Mar 15, 2018 at 14:39
  • Read these: for loop, random number, random string Commented Mar 15, 2018 at 14:43

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.