Answers for "how to check if an element exists in an array of objects js"

3

javascript check if value exists in array of objects

var arr = [{ id: 1, name: 'JOHN' }, 
  { id: 2, name: 'JENNIE'}, 
  { id: 3, name: 'JENNAH' }];

function userExists(name) {
  return arr.some(function(el) {
    return el.name === name;
  }); 
}

console.log(userExists('JOHN')); // true
console.log(userExists('JUMBO')); // false
Posted by: Guest on January-09-2021
0

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')); // true
console.log(userExists('bred')); // false
Posted by: Guest on November-03-2020

Code answers related to "how to check if an element exists in an array of objects js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language