[javascript] FE Dev screening
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- var solution = function (_data, filters) {
- const filteredData = _data.filter(item => {
- let dataPaired = []
- filters.forEach(filter => {
- if (filter.type === 'search') {
- // first we need to split the filter.searchColumn by ',' after that, split and trim each value by '.'
- // doing so we shall have array of arrays and each array of array shall represent keys that can be used to access deeply nested values of _data
- const searchColumn = filter.searchColumn.split(',').map(item => item.split('.').map(item => item.trim()))
- dataPaired = [...dataPaired, searchColumn.some(keys => {
- const searchTerm = filter.value.trim().toLowerCase()
- // since keys is an array of object keys. we shall loop and use each key to get one level deeper value of item object in each iteration
- const columnData = keys.reduce((data, v) => data[v], item)
- // the columnData object has been previously defined to only have string value in each property.
- // Hence this if condition should always be false unless columnData has dynamic definition
- if (Array.isArray(columnData)) {
- // If the column data is an array, we need to check if
- // any of the items in the array contains the search term
- // Although this code block should never execute because of the data definition.
- // we need to convert each of the values of the array to lowerCase and check if any of the values has searchTerm as substring.
- return columnData.some(x => x.toString().toLowerCase().includes(searchTerm))
- } else {
- return String(columnData).toLowerCase().includes(searchTerm)
- }
- })]
- }
- })
- return !dataPaired.includes(false)
- })
- return (filteredData)
- }
Editor
You can edit this paste and save as new: