如何将JavaScript对象转换并映射为正确的对象

huangapple go评论73阅读模式
英文:

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: [{
    &quot;EmployeeName&quot;: {
      &quot;FirstName&quot;: &quot;Joe&quot;,
      &quot;LastName&quot;: &quot;Doe&quot;,
      &quot;Suffix&quot;: null
    },
    &quot;CriticalErrorCount&quot;: 4,
    &quot;HomeDepartment&quot;: &quot;Belgrade&quot;,
    &quot;SupervisorName&quot;: {
      &quot;FirstName&quot;: &quot;Test&quot;,
      &quot;LastName&quot;: &quot;Test&quot;,
      &quot;Suffix&quot;: null
    }
  }]
};

criticalErrorsData = data.criticalErrorsData.map(({
  SupervisorName,
  CriticalErrorCount,
  HomeDepartment,
  EmployeeName
}) =&gt; ({
  supervisor: SupervisorName.FirstName + SupervisorName.LastName,
  department: HomeDepartment,
  errors: CriticalErrorCount,
  employee: EmployeeName.FirstName + EmployeeName.LastName
}));

criticalErrorsData.forEach(item =&gt; console.log(item.supervisor));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 16:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定