3

let users = [
    { id: 11, name: 'Adam', age: 23, group: 'editor' },
    { id: 47, name: 'John', age: 28, group: 'admin' },
    { id: 85, name: 'William', age: 34, group: 'editor' },
    { id: 97, name: 'Oliver', age: 28, group: 'admin' }
  ];

var getFilteredUsers = (array, key, value) => array.filter(x => x[key] === value);
var FilteredUsers = getFilteredUsers(users, "age", 28);
console.log(FilteredUsers);

I am trying to create a dynamic filter, based on the key:value passed in getgetFilteredUsers() will give the corresponding output.

Right now, getgetFilteredUsers() is only computing equals to. But I want to use this same function to compare all three comparisons i.e equals to, less than and greater than.

1
  • Can you define the interface; or, how you would use it when there is a "less than" symbol ? Commented Mar 9, 2019 at 19:05

3 Answers 3

4

An alternative is passing the predicate which will be called by the function filter

let users = [{ id: 11, name: 'Adam', age: 23, group: 'editor' },{ id: 47, name: 'John', age: 28, group: 'admin' },{ id: 85, name: 'William', age: 34, group: 'editor' },{ id: 97, name: 'Oliver', age: 28, group: 'admin' }];
let getFilteredUsers = (array, handler) => array.filter(handler);

let  FilteredUsers = getFilteredUsers(users, x => x['age'] === 28);
console.log(FilteredUsers);

FilteredUsers = getFilteredUsers(users, x => x['age'] > 28);
console.log(FilteredUsers);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

6 Comments

What's the benefit to do this instead of passing the predicate directly to filter()?
@Amessihel hiding logic within the function getFilteredUsers, and that way it follows a DRY principle. It doesn't make sense to create a function for doing only that call of filter, however, the OP wants to add some more logic inside of the function getFilteredUsers.
I understand the "hiding" concept, but not how the DRY principle applies here. Could you please elaborate? It's like we'll repeat the getFilteredUsers() call rather than the filter one.
@Amessihel look at the other answer, see how the condition === string is repeated. That's not a DRY principle. Well, it was removed!
@Ele, Okay I see what you mean: it follows the DRY principle in its implementation. I was just saying you won't reduce the repetition of the filter() call by replacing it with a getFilteredUsers() one. Thank you
|
1

You can use an object to hold your operations and index this object to get the right function to apply for that operation:

const users = [
  { id: 11, name: 'Adam', age: 23, group: 'editor' },
  { id: 47, name: 'John', age: 28, group: 'admin' },
  { id: 85, name: 'William', age: 34, group: 'editor' },
  { id: 97, name: 'Oliver', age: 28, group: 'admin' }
];

const filterUsers = (arr, key, value, op = 'eq') => {
  const ops = {
    eq: (x, y) => x === y,
    lt: (x, y) => x < y,
    gt: (x, y) => x > y
  };
  return arr.filter(x => ops[op](x[key], value));
};

console.log(filterUsers(users, 'age', 28));
console.log(filterUsers(users, 'age', 28, 'lt'));
console.log(filterUsers(users, 'age', 28, 'gt'));

Comments

0

You could take functions for comparison and hand over the wanted one or take equal comparison as default one.

const
    equal = (a, b) => a === b,
    lt = (a, b) => a < b,
    gt = (a, b) => a > b,
    filter = (array, key, value, cf = equal) => array.filter(x => cf(x[key], value));

var users = [{ id: 11, name: 'Adam', age: 23, group: 'editor' }, { id: 47, name: 'John', age: 28, group: 'admin' }, { id: 85, name: 'William', age: 34, group: 'editor' }, { id: 97, name: 'Oliver', age: 28, group: 'admin' }],
    user28 = filter(users, "age", 28),
    userLT28 = filter(users, "age", 28, lt);

console.log(user28);
console.log(userLT28);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.