Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JavaScript multidimensional-array check if typeof is ‘undefined’ without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
If you create a multidimensional-array:
var ThisArray = []; ThisArray["a"] = []; ThisArray["a"]["b"] = []; ThisArray["a"]["b"]["c"] = "This is a string.";
How can you check if ThisArray["a"]["w"]["c"]
for example is defined. Right now I’m doing this:
if (typeof ThisArray !== 'undefined') { if (typeof ThisArray["a"] !== 'undefined') { if (typeof ThisArray["a"]["w"] !== 'undefined') { if (typeof ThisArray["a"]["w"]["c"] !== 'undefined') { // ThisArray["a"]["w"]["c"] is defined! } } } }
How can I do this better and cleaner?
Answer
Use optional chaining:
if (typeof ThisArray?.["a"]?.["w"]?.["c"] !== 'undefined') { // ThisArray["a"]["w"]["c"] is defined! }
As noted in the comments, this is a relatively new language feature, and is not supported by older browsers. See Browser Compatibility.
We are here to answer your question about JavaScript multidimensional-array check if typeof is ‘undefined’ - If you find the proper solution, please don't forgot to share this with your team members.