Skip to main content
edited title
Link

Javascript: Reducing redundancy of notation nested value

added 546 characters in body
Source Link

########UPDATE added lodash get to simplify########

UPDATE added lodash get to simplify

######## UPDATE 2 ########

UPDATE 2
  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    const L1 = _.get(item, prop[0]);
    const L2 = _.get(item, [prop[0], prop[1]]);
    const L3 = _.get(item, [prop[0], prop[1], prop[2]]);
    return prop.length === 1
      ? trim
        ? L1.trim().length > 0
        : L1
      : prop.length === 2
      ? trim
        ? L2.trim().length > 0
        : L2
      : prop.length === 3 && trim
      ? L3.trim().length > 0
      : L3;
  };
UPDATE 3
  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    const nest = [prop[0], prop[1], prop[2]];
    const L1 = _.get(item, nest.slice(0, 1));
    const L2 = _.get(item, nest.slice(0, 2));
    const L3 = _.get(item, nest);
    return prop.length === 1
      ? trim
        ? L1.trim().length > 0
        : L1
      : prop.length === 2
      ? trim
        ? L2.trim().length > 0
        : L2
      : prop.length === 3 && trim
      ? L3.trim().length > 0
      : L3;
  };

########UPDATE added lodash get to simplify########

######## UPDATE 2 ########

  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    const L1 = _.get(item, prop[0]);
    const L2 = _.get(item, [prop[0], prop[1]]);
    const L3 = _.get(item, [prop[0], prop[1], prop[2]]);
    return prop.length === 1
      ? trim
        ? L1.trim().length > 0
        : L1
      : prop.length === 2
      ? trim
        ? L2.trim().length > 0
        : L2
      : prop.length === 3 && trim
      ? L3.trim().length > 0
      : L3;
  };
UPDATE added lodash get to simplify
UPDATE 2
  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    const L1 = _.get(item, prop[0]);
    const L2 = _.get(item, [prop[0], prop[1]]);
    const L3 = _.get(item, [prop[0], prop[1], prop[2]]);
    return prop.length === 1
      ? trim
        ? L1.trim().length > 0
        : L1
      : prop.length === 2
      ? trim
        ? L2.trim().length > 0
        : L2
      : prop.length === 3 && trim
      ? L3.trim().length > 0
      : L3;
  };
UPDATE 3
  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    const nest = [prop[0], prop[1], prop[2]];
    const L1 = _.get(item, nest.slice(0, 1));
    const L2 = _.get(item, nest.slice(0, 2));
    const L3 = _.get(item, nest);
    return prop.length === 1
      ? trim
        ? L1.trim().length > 0
        : L1
      : prop.length === 2
      ? trim
        ? L2.trim().length > 0
        : L2
      : prop.length === 3 && trim
      ? L3.trim().length > 0
      : L3;
  };
added 518 characters in body
Source Link

######## UPDATE 2 ########

  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    const L1 = _.get(item, prop[0]);
    const L2 = _.get(item, [prop[0], prop[1]]);
    const L3 = _.get(item, [prop[0], prop[1], prop[2]]);
    return prop.length === 1
      ? trim
        ? L1.trim().length > 0
        : L1
      : prop.length === 2
      ? trim
        ? L2.trim().length > 0
        : L2
      : prop.length === 3 && trim
      ? L3.trim().length > 0
      : L3;
  };

######## UPDATE 2 ########

  const handleNestFilterProp = (prop: any, item: any, trim?: boolean) => {
    const L1 = _.get(item, prop[0]);
    const L2 = _.get(item, [prop[0], prop[1]]);
    const L3 = _.get(item, [prop[0], prop[1], prop[2]]);
    return prop.length === 1
      ? trim
        ? L1.trim().length > 0
        : L1
      : prop.length === 2
      ? trim
        ? L2.trim().length > 0
        : L2
      : prop.length === 3 && trim
      ? L3.trim().length > 0
      : L3;
  };
added 614 characters in body
Source Link
Loading
Source Link
Loading