1var arr = [{ id: 1, name: 'JOHN' },
2 { id: 2, name: 'JENNIE'},
3 { id: 3, name: 'JENNAH' }];
4
5function userExists(name) {
6 return arr.some(function(el) {
7 return el.name === name;
8 });
9}
10
11console.log(userExists('JOHN')); // true
12console.log(userExists('JUMBO')); // false1var arr = [{ id: 1, username: 'fred' },
2 { id: 2, username: 'bill'},
3 { id: 3, username: 'ted' }];
4
5function userExists(username) {
6 return arr.some(function(el) {
7 return el.username === username;
8 });
9}
10
11console.log(userExists('fred')); // true
12console.log(userExists('bred')); // false
131//Use something like this:
2
3function containsObject(obj, list) {
4 var i;
5 for (i = 0; i < list.length; i++) {
6 if (list[i] === obj) {
7 return true;
8 }
9 }
10
11 return false;
12}
13
14//In this case, containsObject(car4, carBrands) is true. Remove the carBrands.push(car4); call and it will return false instead. If you later expand to using objects to store these other car objects instead of using arrays, you could use something like this instead:
15
16function containsObject(obj, list) {
17 var x;
18 for (x in list) {
19 if (list.hasOwnProperty(x) && list[x] === obj) {
20 return true;
21 }
22 }
23
24 return false;
25}1var obj = {a: 5};
2var array = [obj, "string", 5]; // Must be same object
3array.indexOf(obj) !== -1 // True1 addPerson = (person) => {
2 let filteredPerson = this.state.likes.filter(like => like.name !== person.name);
3 this.setState({
4 likes: [...filteredPerson, person]
5 })
6 }