Today I Learned

hashrocket A Hashrocket project

Delete/Drop a key-value pair from an object

In Javascript, if you want to drop or delete a key-value pair from an object you just pass the object.key to the delete keyword, like this:

// Assume we have an object like this
let object = {
  toyota: ['camry'], 
  honda:  ['accord', 'civic']
}

// It would display something like this
object
=> {toyota: ['camry'], honda: ['accord', 'civic'] }

// We can drop/delete the object value like this
delete object.toyota
=> true

// It would now display something like this
object
=> {honda: ['accord', 'civic']}
See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.