While converting some of my existing project from jQuery to plain JS, I've run into some issues with jQuery's .data() utility. So far, I've been unable to find a plain JS approach that will enable me to get and check all of the data-attributes in the navGlobal object.
var navGlobal = document.querySelector('.nav-global');
if (navGlobal !== null &&
navGlobal.data() &&
navGlobal.data().account &&
navGlobal.data().account.accountData &&
navGlobal.data().account.accountData.address &&
navGlobal.data().account.accountData.address.zip) {
// do something
}
What I've tried:
Initially, I thought I'd check for the existence of these data attributes with .getAttribute() and .hasAttribute(). But this only added to the confusion - I'm not sure how I would use vanilla JS to drill down to the nested properties (i.e., navGlobal.data().account.accountData.address). Chaining .getAttribute() doesn't really work, as most of these data attributes are attached to child elements of navGlobal, and not the navGlobal element itself.
What is the best approach?