-1

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: '[email protected]',
    age: 25,
    address: 'USA'
  },
  {
    name: 'Tom',
    email: '[email protected]',
    age: 35,
    address: 'England'
  },
  {
    name: 'Mark',
    email: '[email protected]',
    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;
2
  • "shorthand": isn't this short already? "efficient": do you have a problem with timings? I mean, old-fashioned for loops are the fastest, but why would you? Moreover, questions about performance and style are more suitable for Code Review Commented Aug 15, 2020 at 9:00
  • You can create a method and pass the parameter on what property you want to filter the data . Commented Aug 15, 2020 at 9:01

4 Answers 4

0

You could use the entries of args.

const entries = Object.entries(args);

return users.filter(user => entries.every(([k, v]) => user[k] === v));
Sign up to request clarification or add additional context in comments.

Comments

0

You could combine the conditionals into one filter statement to only walk through the array once:

function filter(args) {
// initializing list of users
var users = [{
    name: 'John',
    email: '[email protected]',
    age: 25,
    address: 'USA'
  },
  {
    name: 'Tom',
    email: '[email protected]',
    age: 35,
    address: 'England'
  },
  {
    name: 'Mark',
    email: '[email protected]',
    age: 28,
    address: 'England'
  }
];


let conditionals = [];

if (args.name){
    conditionals.push(x => x.name == args.name);
}

if (args.address){
    conditionals.push(x => x.address == args.address);    
}


return users.filter(x => conditionals.every(c => c(x));

}

Please bear in mind that the real-world impact of this optimisation is really low if you are not dealing with enormous amounts of data

Comments

0

You can create helper function for your needs and then use it in your code

const users = [{
    name: 'John',
    email: '[email protected]',
    age: 25,
    address: 'USA'
  },
  {
    name: 'Tom',
    email: '[email protected]',
    age: 35,
    address: 'England'
  },
  {
    name: 'Mark',
    email: '[email protected]',
    age: 28,
    address: 'England'
  }
];

// Filter function
const fltr = (obj, search) => {
  const res = [];
  for(let i = 0; i < obj.length; i++) {
    for(let key in search) {
      let add = true;
      for(let key in search) {
        if(obj[i][key] && obj[i][key] === search[key]) add = true;
        if(!obj[i][key] || obj[i][key] !== search[key]) {
          add = false;
          break;
        }
      }
      if(add) res.push(obj[i]);
    }
  }
  return res;
}

//
// Args
const args = {
  address: "England"
};

// Use your function in your code, fast and easy
const res = fltr(users, args);

// Log
console.log(res);

Comments

0

Maybe this way?

if (args.name || args.address){
    users = users.filter(x => x.name == args.name || x.address == args.address);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.