Filtering an array of objects with javascript map-reduce

let unfilteredUsers = [
{name: 'user1', phone: 'phone1', email: 'email1', country: 'IN'},
{name: 'user2', phone: 'phone2', email: 'email2', country: 'US'},
{name: 'user3', phone: 'phone3', email: 'email3', country: 'CN'},
{name: 'user4', phone: 'phone4', email: 'email4', country: 'UK'},
];

let searchText = 'phone2';
let searchFields = ['name', 'email', 'phone'];
let filteredUsers = unfilteredUsers.filter(user => searchFields
.map(field => user[field].includes(searchText))
.reduce((res1, res2) => res1 || res2, false));

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store