0

How can these checks be made short, crisp and clean?

if (newConfig && 
    newConfig.options &&
    newConfig.options.interaction &&
    newConfig.options.interaction.applyFilteringOnGrid &&
    newConfig.options.interaction.applyFilteringOnGrid.searchFilters) {
            const gridFilters = Object.keys(newConfig.options.interaction.applyFilteringOnGrid.searchFilters);
            for (let i = 0; i < gridFilters.length; i++) {
                    logger.log('Grid Filters used',
                        {gridfilter: `${gridFilters[i]} filter`});
            }
    }
ThinkGeek
  • 4,749
  • 13
  • 44
  • 91
  • if(newConfig.options.interaction.applyFilteringOnGrid.searchFilters) – guru Nov 26 '18 at 13:20
  • This is not correct. – ThinkGeek Nov 26 '18 at 13:21
  • @LokeshAgrawal the duplicate of the comment by guru? – George Nov 26 '18 at 13:22
  • 1
    i suggest to use an array with keys and reduce the object by taking a default object. at the end you need to check just before you try to get the keys. `const getP = (o, k) => (o || {})[k], path = ['options', 'interaction', 'applyFilteringOnGrid', 'searchFilters], gridFilters = Object.keys(path.reduce(getP, newConfig) || {});` – Nina Scholz Nov 26 '18 at 13:25

0 Answers0