DEV Community

Discussion on: How the delete operator can be your super saver

Collapse
 
savagepixie profile image
SavagePixie • Edited

Another way to do it would be mapping over it and taking only the properties we'd like to keep instead of deleting the ones we don't want, which has the advantage of not caring if in the future the object changes and has more properties.

const parseReward = ({ type, discount, duration, eligiblePlans, validity }) => [
  type.toUpperCase(),
  { discount, duration, eligiblePlans, validity }
]

const eligibleRewards = Object.fromEntries(rewards.map(parseRewards))
Collapse
 
blindfish3 profile image
Ben Calder

+1 to this approach. When the anticipated result is a same length array with derived data map() is the best fit; and extracting desired properties is a safer approach than mutating the source object.

I personally like reduce(), but would only use it when both filtering and deriving data.

Collapse
 
code_rams profile image
Ramya Chinnadurai

Hey SavagePixie. Thanks for sharing. Sure will try this approach in near future.