다음이 중첩된 객체라고 가정해 보겠습니다. -
var details = [
{
id:"101",
firstName:"John",
lastName:"Smith",
age:25,
countryName:"US",
subjectDetails: {
subjectId:"Java-101",
subjectName:"Introduction to Java"
},
},
{
"uniqueId": "details_10001"
}
] 중첩된 객체에 액세스하려면 typeOf와 함께 map()을 사용하십시오. 다음은 코드입니다 -
예시
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