DEV Community

loading...

Using the delete Keyword to Remove Object Properties

Randy Rivera
Once I truly put my mind on something, I won't ever stop.
・1 min read
  • Now you know what objects are and their basic features and advantages. In short, they are key-value stores which provide a flexible, intuitive way to structure data, and, they provide very fast lookup time.
  • In earlier posts, we have both added to and modified an object's key-value pairs.
  • Here we will see how we can remove a key-value pair from an object. Let's revisit our foods object example one last time. If we wanted to remove the oranges, plums, and strawberries keys, we can remove it by using the delete keyword like this:
let foods = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};

delete foods.oranges;
delete foods.plums;
delete foods.strawberries;
Enter fullscreen mode Exit fullscreen mode
console.log(foods); // console will display 
{ apples: 25, bananas: 13, grapes: 35 }
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app