2

I believe there's a mechanism for this in CoffeeScript (the ? token), but I'm wondering if there's a better way to do this kind of check in ES6:

if (item && item.integrations && item.integrations.slackData) ...

(besides writing a helper function, which is the immediately obvious solution)

EDIT: The goal here is to make the code is simple and terse as possible.

Example object:

item = { integrations: { slackData: { url: '...' } } };

EDIT 2: Thanks for pointing out the duplicates. I couldn't figure out what terms to search for. I'm probably going to go with using lodash's _.get() function.

8
  • have some example? what if there would be 7 levels hierarchy and we need to check if the deepest key is "truthy" ? Commented Dec 15, 2016 at 20:03
  • "if there's a better way to do this kind of check in ES6" No there isn't. There was a discussion about an operator like this but it didn't lead anywhere. Commented Dec 15, 2016 at 20:04
  • @RomanPerekhrest My post includes the example.. yes. You're right, it could go 7 levels deep, and the idea is to not write out if (a && a.b && a.b.c && a.b.c.d && a.b.c.d.e) Commented Dec 15, 2016 at 20:06
  • Related: javascript test for existence of nested object key Commented Dec 15, 2016 at 20:08
  • data = { integrations: { slackData: { url } = {} } = {} } = item || {};. Commented Dec 15, 2016 at 20:13

1 Answer 1

-2

You can use Array.prototype.every()

var check = [item, item.integrations, item.integrations.slackData]
            .every(function(element) { return element });

Edit, Updated

As noted at comments by @FelixKling, the pattern at above using .every() will fail if item.integrations is undefined.

Given the example object at Question you can use JSON.stringify(), String.prototype.match() with RegExp /"integrations":|"slackData":|"ur‌​l":/g/, then check that .length of resulting array, if any, is equal to the number of properties expected; in the present case 3.

A javascript object which is passed to JSON.stringify() should have properties with the following pattern:

" followed by property followed by " followed by :. Depending on the source object, the RegExp can be further adjusted to meet the specific properties and values of that object.

JSON.stringify(item).match(/"integrations":|"slackData":|"ur‌​l":/g).length === 3

Note that JSON.stringify() has a replacer option which can be used to match properties of the object. The replacer function option is recursive. See also

but I'm wondering if there's a better way to do this kind of check in ES6:

The present Answer does not attempt to indicate that using JSON.stringify() and RegExp to match or filter a javascript object is "better"; but only that the approach can meet the described requirement at the present Question.

Sign up to request clarification or add additional context in comments.

21 Comments

That's a cool idea. Though it's certainly not any less verbose than the code I posted. The goal is to make it far more concise.
That won't work if item.integrations doesn't exist. It doesn't make sense at all unfortunately. If that would work, then one can just write var check = Boolean(item.integrations.slackData);. Don't understand why this got upvotes :-/
Ah yeah, that too. You would have to make a helper function like checkProp(item, 'item.integrations.slackData'), and then the string would be split apart and each object carefully checked for a truthy value.
@ffxsam Can you include example object at Question?
@FelixKling The point is, with try/catch, you could just say item.integrations.slackData.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.