7

I have an array which have value of id, email and password.

let array = [
 {id: hyu, email: a@a.com, password: 123},
 {id: rft, email: b@b.com, password: 456},
 {id: ght, email: c@c.com, password: 789},
 {id: kui, email: d@d.com, password: 679}
]

Now I would like to return that object when my condition is matched. For that I created a function using javascript some function but I want to return the object and we know that some function return boolean.

I don't have any idea how to do this.

My code is:

const isEmailExists = (email, array) => {
  return array.some(function(el) {
    return el.email === email;
  });
};

if (isEmailExists("c@c.com", array) == true) {
  // I want to work here with returned objects i.e on success case
} else {
  // error case
}

Any help is really appreciated

Aks
  • 1,092
  • 3
  • 12
  • 29
  • You're looking for `.filter()`. – Madara's Ghost Apr 03 '19 at 09:09
  • 4
    Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – Shibon Apr 03 '19 at 09:12

5 Answers5

12

You can make use of .filter() and check if the filtered array has a length of more than 0:

let array = [
 {id: 'hyu', email: 'a@a.com', password: 123},
 {id: 'rft', email: 'b@b.com', password: 456},
 {id: 'ght', email: 'c@c.com', password: 789},
 {id: 'kui', email: 'd@d.com', password: 679}
]

let filtered = array.filter(row => row.email === 'a@a.com');

console.log(filtered);

if (filtered.length > 0) { /* mail exists */ }
else { /* mail does not exist */ }
Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
  • what if we have to make a list of array object and have more conditions? suppose in an array object I need to filter those element whose "name" has "John", "Mark", & "Kevin" and create another array object of only these 3 people, how to achieve this? – Mohit Jan 22 '23 at 12:45
  • You can change the callback function in `filter()` to something like this: `array.filter(row => ["John", "Mark", "Kevin"].includes(row.name));` – Sebastian Kaczmarek Feb 07 '23 at 13:10
12

Assuming the email is unique, you can use find(). This will return null if not email does not exist.

let array = [{"id":"hyu","email":"a@a.com","password":123},{"id":"rft","email":"b@b.com","password":456},{"id":"ght","email":"c@c.com","password":789},{"id":"kui","email":"d@d.com","password":679}];

const getObject = (email, array) => {
  return array.find(function(el) {
    return el.email === email;
  }) || null;
};

console.log(getObject("c@c.com", array));

Shorter Version:

const getObject = (email, array) => array.find(el => el.email === email ) || null;
Eddie
  • 26,593
  • 6
  • 36
  • 58
2

Use find(), not filter(), when you expect zero or one match. find() will return undefined if no match is found and since undefined is falsy and any object is truthy, you can use the return value as a condition for an if clause directly.

const array = [
 {id: 'hyu', email: 'a@a.com', password: 123},
 {id: 'rft', email: 'b@b.com', password: 456},
 {id: 'ght', email: 'c@c.com', password: 789},
 {id: 'kui', email: 'd@d.com', password: 679}
]

const mail = array.find(row => row.email === 'a@a.com');

if (mail) {
  // Do stuff with mail
  console.log(mail);
} else {
  // Handle error
}
JollyJoker
  • 1,256
  • 8
  • 12
0

You could take a function and hand over the key and the value and filter the array.

function getObjects(array, key, value) {
    return array.filter(object => object[key] === value);
}

let array = [{ id: 'hyu', email: 'a@a.com', password: 123 }, { id: 'rft', email: 'b@b.com', password: 456 }, { id: 'ght', email: 'c@c.com', password: 789 }, { id: 'kui', email: 'd@d.com', password: 679 }];

console.log(getObjects(array, 'email', 'c@c.com'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0
const found = array.find(item => (item.email === 'a@a.com')) || null;
console.log(found);
unnamed-bull
  • 361
  • 4
  • 15