check if key values exists inside array of objects js code example
Example 1: how to check if an element exists in an array of objects js
var arr = [{ id: 1, username: 'fred' },
{ id: 2, username: 'bill'},
{ id: 3, username: 'ted' }];
function userExists(username) {
return arr.some(function(el) {
return el.username === username;
});
}
console.log(userExists('fred'));
console.log(userExists('bred'));
Example 2: how to check if a key exists in an object javascript
!("key" in obj)
!"key" in obj
Example 3: check if a key exists in an object javascript
"key" in obj