Iterating and printing a JSON with no initial key and multiple entries?


For iterating and printing, use forEach() loop in JavaScript.

Example

const details =[
   {
      "studentId":101,
      "studentName": "John Doe",
   },
   {
      "studentId":102,
      "studentName": "David Miller",
   },
];
details.forEach(obj=>{
   console.log("StudentId="+obj.studentId);
   console.log("StudentName="+obj.studentName);
})

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo174.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo174.js
StudentId=101
StudentName=John Doe
StudentId=102
StudentName=David Miller

Updated on: 12-Sep-2020

26 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements