[javascript] FE Dev screening

Viewer

copydownloadembedprintName: FE Dev screening
  1.  
  2. var solution = function (_data, filters) {
  3.     const filteredData = _data.filter(item => {
  4.         let dataPaired = []
  5.         filters.forEach(filter => {
  6.             if (filter.type === 'search') {
  7.                 // first we need to split the filter.searchColumn by ',' after that, split and trim each value by '.' 
  8.                 // 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
  9.                 const searchColumn = filter.searchColumn.split(',').map(item => item.split('.').map(item => item.trim()))
  10.                 dataPaired = [...dataPaired, searchColumn.some(keys => {
  11.                     const searchTerm = filter.value.trim().toLowerCase()
  12.  
  13.                     // 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 
  14.                     const columnData = keys.reduce((data, v) => data[v], item)
  15.  
  16.                     // the columnData object has been previously defined to only have string value in each property. 
  17.                     // Hence this if condition should always be false unless columnData has dynamic definition
  18.                     if (Array.isArray(columnData)) {
  19.                         // If the column data is an array, we need to check if 
  20.                         // any of the items in the array contains the search term
  21.  
  22.                         // Although this code block should never execute because of the data definition. 
  23.                         // we need to convert each of the values of the array to lowerCase and check if any of the values has searchTerm as substring.
  24.                         return columnData.some(=> x.toString().toLowerCase().includes(searchTerm))
  25.  
  26.                     } else {
  27.                         return String(columnData).toLowerCase().includes(searchTerm)
  28.                     }
  29.                 })]
  30.             }
  31.         })
  32.         return !dataPaired.includes(false)
  33.     })
  34.     return (filteredData)
  35. }

Editor

You can edit this paste and save as new:


File Description
  • FE Dev screening
  • Paste Code
  • 24 Mar-2023
  • 2.03 Kb
You can Share it: