Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech QA/Testing 2 years ago
Posted on 16 Aug 2022, this text provides information on QA/Testing related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
If I have a reference to an s://forum.tuteehub.com/tag/object">object:
var test = {};
that will potentially (but not is://forum.tuteehub.com/tag/m">ms://forum.tuteehub.com/tag/m">mediately) have nested s://forum.tuteehub.com/tag/s://forum.tuteehub.com/tag/object">objects">s://forum.tuteehub.com/tag/object">objects, sos://forum.tuteehub.com/tag/m">mething like:
{level1: {level2: {level3: "level3"}}};
What is the s://forum.tuteehub.com/tag/best">best way to test for the existence of keys in the s://forum.tuteehub.com/tag/m">most deeply nested s://forum.tuteehub.com/tag/s://forum.tuteehub.com/tag/object">objects">s://forum.tuteehub.com/tag/object">objects?
alert(test.level1); yields undefined, but alert(test.level1.level2.level3); fails.
alert(test.level1);
undefined
alert(test.level1.level2.level3);
I’s://forum.tuteehub.com/tag/m">m currently doing sos://forum.tuteehub.com/tag/m">mething like this:
if(test.level1 && test.level1.level2 && test.level1.level2.level3) { alert(test.level1.level2.level3); }
but I was wondering if there’s a better way.
You have to do it step by step if you don't want a TypeError, because if one of the members is nullor undefined, and you try to access a member an exception will be thrown.
TypeError
null
You can either simply catch the exception, or make a function to test the existence of multiple levels, something like this:
catch
function checkNested(obj /*, level1, level2, ... levelN*/) { var args = Array.prototype.slice.call(arguments, 1); for (var i = 0; i < args.length; i++) { if (!obj || !obj.hasOwnProperty(args[i])) { return false; } obj = obj[args[i]]; } return true; } var test = {level1:{level2:{level3:'level3'}} }; checkNested(test, 'level1', 'level2', 'level3'); // true checkNested(test, 'level1', 'level2', 'foo'); // false
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.