简体   繁体   中英

How to remove key value pair from an object having values as another array of objects?

Here I have created a data based on my backend get call response. So I came across usecase where my data have to match with my backend data type.

The data I have generated looks like this:

var valGroup = {
    "Tier1": [
        {
            "DE": 0,
            "CreditRange": "0",
            "VintageCriteria": [],
            "DBR": 0,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0,
            "category": "Tier1"
        },
        {
            "DE": 0,
            "CreditRange": "0",
            "VintageCriteria": [],
            "DBR": 0.8,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0,
            "category": "Tier1"
        }
    ],
    "Tier2": [
        {
            "DE": 0,
            "CreditRange": "0",
            "VintageCriteria": [],
            "DBR": 0,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0,
            "category": "Tier2"
        },
        {
            "DE": 0,
            "CreditRange": "",
            "VintageCriteria": [],
            "DBR": 0,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0,
            "category": "Tier2"
        }
    ]
}

Here from the above mentioned data I have to remove the "category" key value pair like this:

var valGroup = {
    "Tier1": [
        {
            "DE": 0,
            "CreditRange": "0",
            "VintageCriteria": [],
            "DBR": 0,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0
        },
        {
            "DE": 0,
            "CreditRange": "0",
            "VintageCriteria": [],
            "DBR": 0.8,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0
        }
    ],
    "Tier2": [
        {
            "DE": 0,
            "CreditRange": "0",
            "VintageCriteria": [],
            "DBR": 0,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0
        },
        {
            "DE": 0,
            "CreditRange": "",
            "VintageCriteria": [],
            "DBR": 0,
            "NetsalesCriteria": [],
            "ABBmultiplier": 0,
            "BTO": 0,
            "MUE": [],
            "TurnoverCriteria": 0,
            "DSCR": 0
        }
    ]
}

I tried something with Object.keys and going through my object's key and index but can't delete that key/value pair. Is there any other approach by which I can get the required data?

Here is a line that shows how it could be done:

Object.keys(valGroup).forEach(key => valGroup[key].forEach(arrayElement => delete arrayElement['category']))

You can use it to create a function or whatever you'd prefer. Here is another thread that shows more examples: How do I remove a key from a JavaScript object?

Since you mentioned that you tried that with object.keys() ... here is a solution how that would look like:

 var valGroup = { "Tier1": [ { "DE": 0, "CreditRange": "0", "VintageCriteria": [], "DBR": 0, "NetsalesCriteria": [], "ABBmultiplier": 0, "BTO": 0, "MUE": [], "TurnoverCriteria": 0, "DSCR": 0, "category": "Tier1" }, { "DE": 0, "CreditRange": "0", "VintageCriteria": [], "DBR": 0.8, "NetsalesCriteria": [], "ABBmultiplier": 0, "BTO": 0, "MUE": [], "TurnoverCriteria": 0, "DSCR": 0, "category": "Tier1" } ], "Tier2": [ { "DE": 0, "CreditRange": "0", "VintageCriteria": [], "DBR": 0, "NetsalesCriteria": [], "ABBmultiplier": 0, "BTO": 0, "MUE": [], "TurnoverCriteria": 0, "DSCR": 0, "category": "Tier2" }, { "DE": 0, "CreditRange": "", "VintageCriteria": [], "DBR": 0, "NetsalesCriteria": [], "ABBmultiplier": 0, "BTO": 0, "MUE": [], "TurnoverCriteria": 0, "DSCR": 0, "category": "Tier2" } ] } const result = Object.fromEntries([...Object.keys(valGroup).map(key => [key, valGroup[key].map(({category, ...rest}) => rest)]) ]) console.log(result);

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