Abhinav Ch Abhinav Ch - 1 month ago 9
JSON Question

Printing users details which are in JSON format Like foreach in php

Since I'm new to NodeJS, I dont understand how to print all users and their details fettched from database which is in JSON format.

Like in PHP

foreach($users as $user){
echo $user->user_name;
}


how to fetch user details one by one like above or any other way if the JSON data fetched from db is shown like below:

data = [{"uid":14,"name":"Sam"},{"uid":17,"name":"Sid"},{"uid":19,"name":"Saz"}];

Answer

You can use normal for loop for the above task.

for (var i=0; i<data.length; i++) {
  console.log(data[i].uid)
  console.log(data[i].name)
}

The result will look like

uid: 14 name:Sam
uid: 17 name:Sid
uid: 19 name:Saz
Comments