英文:
How to convert and map javascript object to a proper one
问题
我有一个名为 data
的主对象。在 data
对象下,我有一个名为 criticalErrorsData
的关键数据,它是一种数组类型。在该对象内部,我有多个像这样的对象:
[
{
"EmployeeName": {
"FirstName": "Joe",
"LastName": "Doe",
"Suffix": null
},
"CriticalErrorCount": 4,
"HomeDepartment": "Belgrade",
"SupervisorName": {
"FirstName": "Test",
"LastName": "Test",
"Suffix": null
}
}
]
我需要对 data.criticalErrorsData
内的所有这些项目执行 .forEach()
,并将其映射如下所示:
const criticalErrorsData = [
{ supervisor: SupervisorName.FirstName + SupervisorName.LastName, department: HomeDepartment, errors: CriticalErrorCount, employee: EmployeeName.FirstName + EmployeeName.LastName },
{ ... },
{ ... }
];
我该如何实现这个目标?我是否只需要使用 .map()
函数,还是需要引入一个辅助变量?
我的对象结构示例如下:
data
{
CriticalErrorsData:
{
[0] - 我想要映射此对象
[1] - 我想要映射此对象
[2] - 我想要映射此对象
... - 与 CriticalErrorsData 中的数量相同
}
}
我也尝试了类似这样的方法:
const test = receivedData.criticalErrorsData.map(obj => ({
supervisor: obj.SupervisorName.FirstName + obj.SupervisorName.LastName,
department: obj.HomeDepartment,
errors: obj.CriticalErrorCount,
employee: obj.EmployeeName.FirstName + obj.EmployeeName.LastName
}));
console.log(test);
如果您需要进一步的帮助,请告诉我。
英文:
I have main object called data
. Under data
object I have criticalErrorsData which is a type of array.
Inside that object I have multiple objects like this:
[
{
"EmployeeName": {
"FirstName": "Joe",
"LastName": "Doe",
"Suffix": null
},
"CriticalErrorCount": 4,
"HomeDepartment": "Belgrade",
"SupervisorName": {
"FirstName": "Test",
"LastName": "Test",
"Suffix": null
}
}
]
I need to perform .forEach()
for all these items inside data.criticalErrorsData and to map it like this:
const criticalErrorsData = [
{ supervisor:SupervisorName.FirstName + SupervisorName.LastName, department: HomeDepartment, errors: CriticalErrorCount, employee: EmployeeName.FirstName+EmployeeName.LastName },
{ ... },
{ ... }
];
How can I achieve this? Sould I need to use only.map() function or I need to introduce a helper variable also?
Example of structure of my objects:
data
{
CriticalErrorsData:
{
[0] - this object I want to map
[1] - this object I want to map
[2] - this object I want to map
... - as many as CriticalErrorsData has them
}
}
I also tried something like this:
const test = receivedData.criticalErrorsData.map(obj => ({
supervisor: obj.SupervisorName.FirstName + obj.SupervisorName.LastName,
department: obj.HomeDepartment,
errors: obj.CriticalErrorCount,
employee: obj.EmployeeName.FirstName + obj.EmployeeName.LastName
})
);
console.log(test);
答案1
得分: 1
你可以使用 map()
来实现。在参数列表中使用解构赋值将允许你以你展示的方式引用属性。
const data = {
criticalErrorsData: [{
"EmployeeName": {
"FirstName": "Joe",
"LastName": "Doe",
"Suffix": null
},
"CriticalErrorCount": 4,
"HomeDepartment": "Belgrade",
"SupervisorName": {
"FirstName": "Test",
"LastName": "Test",
"Suffix": null
}
}]
};
criticalErrorsData = data.criticalErrorsData.map(({
SupervisorName,
CriticalErrorCount,
HomeDepartment,
EmployeeName
}) => ({
supervisor: SupervisorName.FirstName + SupervisorName.LastName,
department: HomeDepartment,
errors: CriticalErrorCount,
employee: EmployeeName.FirstName + EmployeeName.LastName
}));
criticalErrorsData.forEach(item => console.log(item.supervisor));
注意:上面的代码已经是翻译好的内容,不包含其他内容。
英文:
You can do it with map()
. Destructuring in the parameter list will allow you to refer to the properties the way you show.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data = {
criticalErrorsData: [{
"EmployeeName": {
"FirstName": "Joe",
"LastName": "Doe",
"Suffix": null
},
"CriticalErrorCount": 4,
"HomeDepartment": "Belgrade",
"SupervisorName": {
"FirstName": "Test",
"LastName": "Test",
"Suffix": null
}
}]
};
criticalErrorsData = data.criticalErrorsData.map(({
SupervisorName,
CriticalErrorCount,
HomeDepartment,
EmployeeName
}) => ({
supervisor: SupervisorName.FirstName + SupervisorName.LastName,
department: HomeDepartment,
errors: CriticalErrorCount,
employee: EmployeeName.FirstName + EmployeeName.LastName
}));
criticalErrorsData.forEach(item => console.log(item.supervisor));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论