{"id":136381,"date":"2024-07-12T05:11:48","date_gmt":"2024-07-12T05:11:48","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=136381"},"modified":"2024-07-12T05:15:36","modified_gmt":"2024-07-12T05:15:36","slug":"for-a-given-below-object","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/uncategorized\/for-a-given-below-object\/","title":{"rendered":"For a given below object"},"content":{"rendered":"\n<p>const exampleObj = {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; taxi:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;a car licensed to transport passengers in return for payment of a fare&#8221;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; food: {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sushi:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;a traditional Japanese dish of prepared rice accompanied by seafood and vegetables&#8221;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; apple: {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Honeycrisp:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;an apple cultivar developed at the MAES Horticultural Research Center&#8221;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fuji:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;an apple cultivar developed by growers at Tohoku Research Station&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp; };<\/p>\n\n\n\n<p>Print the above object in a nested structure and make sure the code supports deep nested structure<\/p>\n\n\n\n<p>Below is the UI design for the give object:-<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Solution: 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const exampleObj = {\n  taxi: \"a car licensed to transport passengers in return for payment of a fare\",\n  food: {\n    sushi: \"a traditional Japanese dish of prepared rice accompanied by seafood and vegetables\",\n    apple: {\n      Honeycrisp: \"an apple cultivar developed at the MAES Horticultural Research Center\",\n      Fuji: \"an apple cultivar developed by growers at Tohoku Research Station\"\n    }\n  }\n};\n\nfunction printNestedObject(obj, level = 0) {\n  const indent = '  '.repeat(level); \/\/ Indentation based on the current level\n\n  for (const &#91;key, value] of Object.entries(obj)) {\n    if (typeof value === 'object' &amp;&amp; value !== null) {\n      console.log(`${indent}${key}:`);\n      printNestedObject(value, level + 1); \/\/ Recursive call for nested objects\n    } else {\n      console.log(`${indent}${key}: ${value}`);\n    }\n  }\n}\n\nprintNestedObject(exampleObj);\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Solution: 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const exampleObj = {\n  taxi: \"a car licensed to transport passengers in return for payment of a fare\",\n  food: {\n    sushi: \"a traditional Japanese dish of prepared rice accompanied by seafood and vegetables\",\n    apple: {\n      Honeycrisp: \"an apple cultivar developed at the MAES Horticultural Research Center\",\n      Fuji: \"an apple cultivar developed by growers at Tohoku Research Station\"\n    }\n  }\n};\n\n\/\/ Higher-order function to print nested objects\nconst printNestedObject = (obj, format = (key, value, indent) => `${indent}${key}: ${value}`, level = 0) => {\n  const indent = '  '.repeat(level);\n\n  \/\/ Using forEach as the higher-order function to iterate over the object entries\n  Object.entries(obj).forEach((&#91;key, value]) => {\n    if (typeof value === 'object' &amp;&amp; value !== null) {\n      console.log(`${indent}${key}:`);\n      printNestedObject(value, format, level + 1);  \/\/ Recursive call with incremented level\n    } else {\n      console.log(format(key, value, indent));  \/\/ Use the format function for the output\n    }\n  });\n};\n\n\/\/ Usage\nprintNestedObject(exampleObj);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>const exampleObj = { &nbsp;&nbsp;&nbsp; taxi: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;a car licensed to transport passengers in return for payment of a fare&#8221;, &nbsp;&nbsp;&nbsp; food: { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sushi: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;a traditional Japanese dish of prepared rice accompanied by seafood and vegetables&#8221;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; apple: { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Honeycrisp: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;an apple cultivar developed at the MAES Horticultural Research Center&#8221;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-136381","post","type-post","status-publish","format-standard","","category-uncategorized"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-ztH","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136381"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=136381"}],"version-history":[{"count":6,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136381\/revisions"}],"predecessor-version":[{"id":136387,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136381\/revisions\/136387"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=136381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=136381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=136381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}