I am learning NodeJS . I am using a JSON Object to check if a user is present.
This is my JSON (users.json):
{
"users": [{
"fname": "Robert",
"lname": "Downey Jr.",
"password": "ironman"
}, {
"fname": "Chris",
"lname": "Evans",
"password": "cap"
}, {
"fname": "Chris",
"lname": "Hemsworth",
"password": "thor"
}, {
"fname": "Jeremy",
"lname": "Renner",
"password": "hawk"
}]
}
Now I want to pass the fname value of one entry and see if it exists in the JSON Object.
This is what I'm doing :
var file = JSON.parse(fs.readFileSync('users.json', 'utf8'));
for (eachUser in file.users) {
console.log(eachUser.fname);
}
But I do not get the fname values but just undefined
. I don't know what I am doing wrong.
Also is there a way to find if the value exists without having to iterate over the JSON Object ?