Skip to content

Instantly share code, notes, and snippets.

@webpapaya
Last active March 2, 2020 13:27
Show Gist options
  • Save webpapaya/200e1792cc114a47909926539916f4dd to your computer and use it in GitHub Desktop.
Save webpapaya/200e1792cc114a47909926539916f4dd to your computer and use it in GitHub Desktop.

First level

Filter all names by their first character of the given array.

["Hans", "Mike", "Fabian", "Anna"]
  • "H" returns ["Hans"]
  • "M" returns ["Mike"]
  • "F" returns ["Fabian"]

const filterMembers = (members, search) => { // todo }

Second level

Enhance the previous example and filter all names by any character

  • "A" returns ["Hans", "Fabian", "Anna"]
  • "i" returns ["Mike", "Fabian"]

Third level

The API endpoint changed. Instead of returning a list of strings it now returns a list of objects. The behaviour should stay the same as in the previous exercise.

[{ first: "Hans" }, { first: "Mike" }, { first: "Fabian" }, { first: "Anna" }]
  • "A" returns [{ first: "Hans"}, { first: "Fabian" }, { first: "Anna" }]
  • "i" returns [{ first: "Mike" }, { first: "Fabian" }]

Fourth level

The API Endpoint now returns the last name as well. Our filter function should filter by first and last name.

[
  { first: "Hans", last: "Müller" },
  { first: "Mike", last: "Eibensteiner" },
  { first: "Fabian", last: "Hofer" },
  { first: "Anna", last: "Huber" },
]
const includedInString = (stringToBeSearchedIn, search) =>
(stringToBeSearchedIn || '').toLowerCase().indexOf((search || '').toLowerCase()) !== -1;
const searchMember = (search) => (member) => Object.keys(member)
.some((key) => includedInString(member[key], search));
const filterMembers = (members = [], search = '') =>
members.filter(searchMember(search));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment