My HTML input consists of the following data:
{
administrator: true,
premium: true,
name: 'John',
city: 'Bucharest'
},{
administrator: false,
premium: true,
name: 'Marry',
city: 'London'
},{
administrator: false,
premium: false,
name: 'Arya',
city: 'Bucharest'
}
I want to output every element with city === 'Bucharest' like this:
All people from Bucharest are:
John
Arya
I have attempted the following code:
var users = document.getElementById("oneUser").innerText;
users.forEach(function (user){
console.log(user);
})
However, it resulted in an error:
Uncaught TypeError: users.forEach is not a function
I also tried to use JSON.parse(), but the input structure does not match a valid JSON type due to missing double quotes in the first parameter.
EDIT:
<p id="oneUser" style="display:none"><%= users %></p>
The <%= users %> array originates from a mongodb database.
Initially, I assigned it to a JavaScript variable which is why it is now stored as a String.
var users = document.getElementById("oneUser").innerText;