Skip to content

Latest commit

 

History

History
20 lines (20 loc) · 477 Bytes

JavaScript.md

File metadata and controls

20 lines (20 loc) · 477 Bytes

JavaScript

1. Check nested object

checkAll = (data, key) => {
  return key.split(".").reduce((o, k) => {
    return (typeof o === 'undefined' || o === null) ? o : o[k];
  }, data)
}

Example, if we want to check error response from axios, we could call with:

axios.get()
  .then()
  .catch(e => {
    const message = this.checkAll(e, "response.data.message");
    if (message !=== undefined && ...) {
      // Do something
    }
  })