How delete operator can be your super saver
The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.
by rams_codes
JavaScript
var eligible_rewards = [
{
_id: "5d6fcdf63eb318a9d6981f35",
type: "welcome_offer",
discount: 15,
status: true,
validity: "05-09-2020"
},
{
_id:"5d779b833eb318a9d6f60528",
type: "extend_subscription",
duration: 3,
status: true,
validity: "05-09-2020"
},
{
_id: "5d7b700b3eb318a9d6227637",
type: "unlock_course",
eligiblePlans: [ "js-combo", "react-combo"],
status: true,
validity: "05-09-2020"
}
]
const rewards = eligible_rewards.reduce( ( acc, val) => {
const type = val.type.toUpperCase();
const details = {
[type]: {
...val
}
}
delete details[type].type;
delete details[type].status;
delete details[type]._id;
return {...acc, ...details}
},{})
console.log(rewards);