Skip to content

Instantly share code, notes, and snippets.

@pmkhoa
Created October 25, 2017 23:51
Show Gist options
  • Save pmkhoa/e492e68e1509cc9f0a2982d509f3e7d7 to your computer and use it in GitHub Desktop.
Save pmkhoa/e492e68e1509cc9f0a2982d509f3e7d7 to your computer and use it in GitHub Desktop.
export default function filterTenants(tenants, search) {
return tenants.filter(tenant => {
let hasFound = false
// For convenient, we allow user to search tenant by name & code
if (
tenant.name.toLowerCase().includes(search) ||
tenant.code.toLowerCase().includes(search)
) {
hasFound = true
}
// When there is a children, call filterTenants to find other matches.
if (Array.isArray(tenant.children) && tenant.children.length > 0) {
// Create temp to hold recursive result.
const temp = filterTenants(tenant.children, search)
if (temp.length) {
/* eslint-disable no-param-reassign */
tenant.children = temp
hasFound = true
}
}
return hasFound
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment