I have this array
let registers = [Register, Register, Register];
and
Register {name: 'Ray', email: 'ray@gmail.com', password: '1234'}
Register {name: 'Carl', email: 'carl@gmail.com', password: '4321'}
Register {name: 'Lina', email: 'lina@outlook.com', password: '5678'}
How can I get a new array with the objects that have @gmail.com as email ?
Use Array.filter with String.includes.
Array.filter filters the original array with some condition, where String.includes provides the condition, the email includes a string @gmail.com
const data = [{ name: 'Ray', email: 'ray@gmail.com', password: '1234' },
{ name: 'Carl', email: 'carl@gmail.com', password: '4321' },
{ name: 'Lina', email: 'lina@outlook.com', password: '5678' }];
const output = data.filter(node => node.email.includes('@gmail.com'));;
console.log(output);
You can use filter and regex here /@gmail\.com$/ to get only the elements that ends with gmail.com
const data = [
{ name: "Ray", email: "ray@gmail.com", password: "1234" },
{ name: "Carl", email: "carl@gmail.com", password: "4321" },
{ name: "Lina", email: "lina@outlook.com", password: "5678" },
];
const output = data.filter((o) => o.email.match(/@gmail\.com$/));
console.log(output);