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

时间:2019-11-22 11:26:08

标签: javascript arrays dictionary lodash

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

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"}]

关于如何执行此操作的任何想法?

1 个答案:

答案 0 :(得分:0)

首先,您需要修复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);