0

I've got the following code:

const result = A.field_1.subfield_2

How can I evaluate result to null when A in null (or A.field_1) and avoid null errors? I was wondering if there's a more elegant approach rather than doing

if (A != null && A.field_1 != null) { ...

E.g., what if there're 10 fields.

A. Karnaval
  • 727
  • 2
  • 8
  • 12
  • `if (A != null && A.field_1 != null){}` ? – Calvin Nunes Jul 17 '19 at 16:05
  • @CalvinNunes that makes sense, but I was wondering if there's a more elegant approach. – A. Karnaval Jul 17 '19 at 16:07
  • let y=A && A.field_1 && A.field_1.subfield_2 – kunal verma Jul 17 '19 at 16:07
  • If you don't want to have to null check the whole way down, consider [lodash#get](https://lodash.com/docs/#get). I wish there was a built-in way to do this, but there isn't right now. – zero298 Jul 17 '19 at 16:09
  • The [optional chaining](https://github.com/tc39/proposal-optional-chaining) and [nullish coalescing](https://github.com/tc39/proposal-nullish-coalescing) proposals might be of interest to you. If you transpile your application with Babel, they [both](https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator) have [plugins](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining) you can use. – Patrick Roberts Jul 17 '19 at 16:13

4 Answers4

1
if(A && A.field_1 && A.field_1.subfield_2) {
  // Your code here
  // Remember: null and undefined always return false
}
Robin
  • 4,902
  • 2
  • 27
  • 43
0

Create custom method to check Object. Using object constructor we can check weather a variable is object or not.

function isObject(obj) {
    return (!!obj) && obj.constructor === Object
}
const result = isObject(A) && isObject(A.field_1) ? A.field_1.subfield_2 : null
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

Wrap it in a try/catch, works for 10 or more as well :)

try {
  const result = A.field_1.subfield_2
} catch(err) {
  // do something with the error
}
emcee22
  • 1,851
  • 6
  • 23
  • 32
  • 1
    Exception handling shouldn't be used for control flow. This is convenient, but its an anti-pattern. –  Jul 17 '19 at 16:24
0

You can make this a recursive function by looping over all of the Object.values() and testing if they are valid or not using every().

The following function handles objects and arrays:

function test(obj) {
  if (typeof obj == 'undefined' || obj == null) return false
  if (typeof obj == 'object') {
    return Object.values(obj).every(i => test(i))
  }
  return true
}

console.log(test({field_1: 0, field_2: 'abc'}))
console.log(test({field_1: 1, field_2: null}))
console.log(test({field_1: 1, field_2: null, cat: 123}))
console.log(test({field_1: 1, field_2: /abc/g, monkey: { dog: 'cat' }}))
console.log(test({field_1: 1, field_2: /abc/g, monkey: { dog: 'cat', mouse: { pig: null } }}))
console.log(test({field_1: 1, field_2: /abc/g, monkey: { dog: 'cat', mouse: { pig: [{cat: 'af'}]}}}))
console.log(test({field_1: 1, field_2: /abc/g, monkey: { dog: 'cat', mouse: { pig: [{cat: 'af'}, {cat: null}]}}}))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338