Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #check #element #exists #array #objects #js
ADD COMMENT
Topic
Name
9+8 =