1

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?

2
  • Please show your exact attempt and the actual result. Was there an error in the console? Commented Aug 11, 2022 at 13:53
  • var data = Object.keys(valGroup).forEach(async function(key, index) { await console.log(key,valGroup[key],"OE",fullData); }); valGroup[key] returns every main object's value, that is array of objects From valGroup[key] I tried to filter the category and delete it. But it's throwing undefined error. Commented Aug 11, 2022 at 13:58

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried this now in my local system, it's showing undefined error on console. jsfiddle.net/Lv197fw2
You missed to iterate through the array of objects so I acceped the below answer whick works for me.
1

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

1 Comment

Works fine on my local. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.