简体   繁体   中英

How to create new object by making a value as key from another object?

I have an object and I'm trying to create a new object by taking all the identical values as keys. For example,

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

I have to get my new object by using stateName as key like below:

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

Any ideas on how to do this?

First, you need to fix your data property as it is not valid. Then you just loop through the array and generate a new object based on the stateName property.

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM