I'm trying store the data about users to array in JavaScript. Problem is that i want to add user to array when his ID isn't in that array, yet.
var users = [
{
id: 2,
name: "Kevin"
},
{
id: 1,
name: "Jason"
}
];
for(var i = 0; i < users.length; i++){
if(users[i].id != 1){
users.push({
id: 1,
name: "Adam"
);
}
}
It's broken because first user with ID 2 is not equal to 1, but i don't know how to fix it. How it should look:
if(userID isnt in array users){
// add user to array
}
Can anyone help me with that, please? Thanks.