Skip to content

Instantly share code, notes, and snippets.

@vipulwairagade
Created November 3, 2020 10:42
Show Gist options
  • Save vipulwairagade/423756afa5929a771aa417378a1a3807 to your computer and use it in GitHub Desktop.
Save vipulwairagade/423756afa5929a771aa417378a1a3807 to your computer and use it in GitHub Desktop.
Keep only selected keys from a particular object in Javascript
let member = {
id: 178,
identifier: "User2",
firstName: "Training",
lastName: "User2",
inactiveDate: "2007-09-10T18:52:28Z",
inactiveFlag: true,
officeEmail: "test@test.com",
defaultEmail: "Office",
primaryEmail: "test@test.com",
defaultPhone: "Office",
}
let fields = ["id", "firstName", "lastName"]
const getReducedObject = (member, fields) => fields.reduce((acc, curr) => {
acc[curr] = member[curr]
return acc
}, {})
member = getReducedObject(member, fields)
console.log(member)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment