I have two arguments that are optional that I want to use to filter the array.
As they are optional i am using two if statements, but I am curious if there is a more efficient/shorthand way.
// initializing list of users
var users = [{
name: 'John',
email: 'johnson@mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom@mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark@mail.com',
age: 28,
address: 'England'
}
];
if (args.name){
users = users.filter(x => x.name == args.name);
}
if (args.address){
users = users.filter(x => x.address == args.address);
}
return users;