gpt4 book ai didi

javascript - 如何通过将另一个对象的值作为键来创建新对象?

转载 作者:行者123 更新时间:2023-12-02 22:28:25 27 4
gpt4 key购买 nike

我有一个对象,我正在尝试通过将所有相同的值作为键来创建一个新对象。例如,

data = [
{
"sum(PendingBenNotValidated)": 35.0,
"stateName": "Andaman & Nicobar Islands",
"diagMonth": 4,
"diagYear": 2018,
"TypeOfPatient": "IndiaTbPrivate"
},
{
"sum(PendingBenNotValidated)": 8.0,
"stateName": "Andaman & Nicobar Islands",
"diagMonth": 5,
"diagYear": 2018,
"TypeOfPatient": "IndiaTbPrivate"
},
{
"sum(PendingBenNotValidated)": 19.0,
"stateName": "Andaman & Nicobar Islands",
"diagMonth": 7,
"diagYear": 2018,
"TypeOfPatient": "IndiaTbPrivate"
},
{
"sum(PendingBenNotValidated)": 16.0,
"stateName": "Andaman & Nicobar Islands",
"diagMonth": 8,
"diagYear": 2018,
"TypeOfPatient": "IndiaTbPrivate"
},
{
"sum(PendingBenNotValidated)": 1.0,
"stateName": "Andhra Pradesh",
"diagMonth": 1,
"diagYear": 2018,
"TypeOfPatient": "IndiaTbPrivate"
},
{
"sum(PendingBenNotValidated)": 0.0,
"stateName": "Andhra Pradesh",
"diagMonth": 2,
"diagYear": 2018,
"TypeOfPatient": "IndiaTbPrivate"
}]

我必须使用 stateName 作为键来获取新对象,如下所示:

new data = [{Andaman & Nicobar Islands: [{PendingBenNotValidated: 35, diagMonth: 4, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},{PendingBenNotValidated: 19, diagMonth: 7, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"}], Andhra Pradesh: [{PendingBenNotValidated: 0, , diagMonth: 2, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"}]

关于如何做到这一点有什么想法吗?

最佳答案

首先,您需要修复您的 data 属性,因为它无效。然后,您只需循环遍历该数组并根据 stateName 属性生成一个新对象。

var data = [
{PendingBenNotValidated: 35, stateName: "Andaman & Nicobar Islands", diagMonth: 4, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 8, stateName: "Andaman & Nicobar Islands", diagMonth: 5, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 19, stateName: "Andaman & Nicobar Islands", diagMonth: 7, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 16, stateName: "Andaman & Nicobar Islands", diagMonth: 8, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 9, stateName: "Andaman & Nicobar Islands", diagMonth: 9, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 9, stateName: "Andaman & Nicobar Islands", diagMonth: 10, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 16, stateName: "Andaman & Nicobar Islands", diagMonth: 11, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 12, stateName: "Andaman & Nicobar Islands", diagMonth: 12, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 1, stateName: "Andhra Pradesh", diagMonth: 1, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"},
{PendingBenNotValidated: 0, stateName: "Andhra Pradesh", diagMonth: 2, diagYear: 2018, TypeOfPatient: "IndiaTbPrivate"}];

let entries = {};
data.map( (e) => entries[e.stateName] = e );
console.log(entries);

关于javascript - 如何通过将另一个对象的值作为键来创建新对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58993315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com