Safely inspect nested objects with Reduce

Share this video with your friends

Send Tweet

A common problem when dealing with some kinds of data is that not every object has the same nested structure. luke_skywalker.parents.father.is_jedi works, but anakin_skywalker.parents.father.is_jedi throws an exception, because anakin_skywalker.parents.father is undefined. But we can reduce a path to provide safe default values and avoid exceptions when walking the same path on non-homogenous objects - watch to learn how! :)

Mike
Mike
~ 7 years ago

Excellent series, I learned a lot. Looking forward to other series from you.

Wojciech Morawski
Wojciech Morawski
~ 7 years ago

So enlightening! Thank you so much :)

Lars
Lars
~ 7 years ago

This has been great, thanks!

zebin
zebin
~ 6 years ago

Has any assignment or practice questions?

Omid
Omid
~ 6 years ago

Wow! These series are packed with practical information yet very easy to follow. I finally get reduce 💡 You are an amazing teacher Mykola. Thanks!

Yuan Chiang
Yuan Chiang
~ 6 years ago

Amazing series. I'm so surprised to keep learning about (what may appear) "simple" concepts!

~ 3 years ago

Nowadays you can use optional chaining, which has an extra advantage of being type safe if you're using typescript, which it wouldn't if you're just splitting a string to get names of fields.

function fatherWasJedi(character) {
  return character?.parents?.father?.jedi;
}