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

Time:08-11

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?

CodePudding user response:

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?

CodePudding user response:

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

  • Related