The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Advanced Reduce: Safe Nested Object Inspection

Advanced Reduce: Safe Nested Object Inspection

6:29
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! :)
Watch this lesson now
Avatar
egghead.io

A common problem when dealing with some kinds of data is that not every object has the same nested structure. lukeskywalker.parents.father.isjedi works, but anakinskywalker.parents.father.isjedi 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! :)

Avatar
Sean

This series is one of the best tutorials I've seen, and I watch a lot of them. I had "ah ha" moments during just about every one of them - thanks!

In reply to egghead.io
Avatar
mykola

Thanks so much, Sean, that kind of feedback means so much! There's a lot more coming where that came from! :)

In reply to Sean
Avatar
Ronen

hi,

Thanks for the video's i learned allot.

i tried make a small change.

Lets say i want to check if father exists in the object of parent.

I dont understand how to do it with reduce. can you help me out here. Thanks

In reply to egghead.io
Avatar
mykola

That's what this is doing. It checks to see if parents exists on the character, then it checks to see if father exists on parents, then it checks to see if jedi exists on father. It can fail at every step of the way. The reason it works is that you give it the string 'parents.father.jedi' split it on the '.', which gives you an array ['parents', 'father', 'jedi']. It then reduces that array by checking to see if the input object has the first property, and if so returning it and seeing if that returned object has the second property, etc.

If at any point the property is not found, it returns null.

So if you simply want to check for the presence of 'parent.father', remove '.jedi' from the input string that gets parsed to define your path.

Hope that helps!

In reply to Ronen
Avatar
Samir

Same for me: that's the second time I watch this whole course. And again, lot's of great creative use of reduce I forgot about and rediscovered with pleasure. That's why most of us love programming: these 'ah ha' moments. If you feel like making a course on Redux or Cycle.js (as you mentioned them on your stack overflow page) don't hesitate :) Creative usage of RxJS in the same style as what you did with reduce() would be great too :) Happy new year 2017!

In reply to mykola
Avatar
mykola

Thanks, Samir! I love the feedback this course gets! :)

The creators of Redux and Cycle are both Egghead instructors, and I believe they've both got extensive courses here (especially RxJS) - definitely check some of them out! :)

In reply to Samir
Avatar
Samir

Already watched and practiced them several times (sounds like addiction :p ). Ben Lesh, Andre Staltz and Jafar Husain courses - and of course John Linquist's - are great too. But if you feel like adding your personal touch... :)

In reply to mykola
Avatar
Mike

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

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?