在JavaScript中取消嵌套对象数组?
要取消嵌套对象数组,请使用的概念map()
。假设以下是我们的对象数组-
const studentDetails = [{
"studentId": 101,
"studentName": "John",
"subjectDetails": {
"subjectName": "JavaScript"
}
},
{
"studentId": 102,
"studentName": "David",
"subjectDetails": {
"subjectName": "MongoDB"
}
}
];
我们需要取消嵌套subjectName并显示结果。以下是代码-
示例
const studentDetails = [{
"studentId": 101,
"studentName": "John",
"subjectDetails": {
"subjectName": "JavaScript"
}
},
{
"studentId": 102,
"studentName": "David",
"subjectDetails": {
"subjectName": "MongoDB"
}
}
];
const output = studentDetails.map(obj => ({ studentId: obj.studentId,
studentName: obj.studentName, subjectName:obj.subjectDetails.subjectName
}));
console.log(output);
要运行上述程序,您需要使用以下命令-
node fileName.js.
输出结果
在这里,我的文件名为demo92.js。这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo92.js[
{ studentId: 101, studentName: 'John', subjectName: 'JavaScript' },
{ studentId: 102, studentName: 'David', subjectName: 'MongoDB' }
]
以上是 在JavaScript中取消嵌套对象数组? 的全部内容, 来源链接: utcz.com/z/321608.html