मान लें कि निम्नलिखित हमारे नेस्टेड ऑब्जेक्ट हैं -
var details = [
{
id:"101",
firstName:"John",
lastName:"Smith",
age:25,
countryName:"US",
subjectDetails: {
subjectId:"Java-101",
subjectName:"Introduction to Java"
},
},
{
"uniqueId": "details_10001"
}
] नेस्टेड ऑब्जेक्ट्स तक पहुँचने के लिए टाइपऑफ़ के साथ मैप () का उपयोग करें। निम्नलिखित कोड है -
उदाहरण
var details = [
{
id:"101",
firstName:"John",
lastName:"Smith",
age:25,
countryName:"US",
subjectDetails: {
subjectId:"Java-101",
subjectName:"Introduction to Java"
},
},
{
"uniqueId": "details_10001"
}
]
details.map((nestedObject)=>{
if (typeof nestedObject.subjectDetails != 'undefined')
console.log("The subject Name="+nestedObject.subjectDetails.subjectName);
}) उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
node fileName.js.
यहाँ, मेरी फ़ाइल का नाम है demo119.js.
आउटपुट
यह निम्नलिखित आउटपुट देगा -
PS C:\Users\Amit\JavaScript-code> node demo119.js The subject Name=Introduction to Java