DEV Community

Royal Bhati
Royal Bhati

Posted on • Updated on

Remove unwanted properties from Javascript Objects using ES6 destructuring

lets say we received an object from an api response which contains some sensitive data


const userData = {
  name:"Rick Sanchez",
  email:"ricksanchez@getSchwifty.com"
  address :" Dimension C-137",
  sensitiveInfo:"wubba lubaa dub dub",
  secretportalkey:"ABCDEF"
}
Enter fullscreen mode Exit fullscreen mode

Now we want to pass it to some third party api but we dont want to pass the sensitiveInfo and secretPortal key to the Api

So, How do we remove that from the Object ?

Simple, just use the power of destructuring

const {sensitiveInfo,secretportalkey,...notSoSensitiveData} = userData

/* Now our notSoSensitiveData will contain all the properties except the ones which
 are destructured
*/

console.log(notSoSensitiveData)

/* {
  name:"Rick Sanchez",
  email:"ricksanchez@getSchwifty.com"
  address :" Dimension C-137",
}
*/
Enter fullscreen mode Exit fullscreen mode

Top comments (0)