Skip to main content
1 of 5

Reducing redundancy of notation nested value

Hello I have this function that looks for nested value 3 levels deep currently

  // This handle nested data to be applied to filter option which comes from filterProps.prop values
  // This is currently chained 3 level deep I.E clock.props.children to add more just keep chaineding
  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    return trim
      ? prop.length === 1
        ? item[prop[0]] && item[prop[0]].trim().length > 0
        : prop.length === 2
        ? item[prop[0]][prop[1]].trim().length > 0
        : prop.length === 3 && item[prop[0]][prop[1]][prop[2]].trim().length > 0
      : prop.length === 1
      ? item[prop[0]] && item[prop[0]]
      : prop.length === 2
      ? item[prop[0]][prop[1]]
      : prop.length === 3 && item[prop[0]][prop[1]][prop[2]];
  };

I'm trying to figure out a way to reduce the logic and make it simpler. I thinking if it would be a good idea to use get from Lodash https://lodash.com/docs/4.17.15#get