Search code examples
lodashoptional-chaining

convert get of lodash javascript library to optional chaining


I wanted to change this code which is using get method from lodash library to optional chaining method.Can someone help me to convert this

return get(
          this.UIObj,
          'excelList',
          []
        );

Solution

  • Optional return the excelList property of this.UIObj, and if it's undefined (or null) return an empty array using the Nullish coalescing operator (??):

    return this.UIObj?.excelList ?? []