Skip to content

Instantly share code, notes, and snippets.

@wilomgfx
Last active February 13, 2018 16:50
Show Gist options
  • Select an option

  • Save wilomgfx/62cc2cf3d565f5fbedc902f34c58206e to your computer and use it in GitHub Desktop.

Select an option

Save wilomgfx/62cc2cf3d565f5fbedc902f34c58206e to your computer and use it in GitHub Desktop.
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPassword = Object.keys(user)
.filter(key => key !== 'password')
.map(key => ({[key]: user[key]}))
.reduce((accumulator, current) =>
({...accumulator, ...current}),
{}
)
;
// userWithoutPassword becomes {name: 'Shivek Khurana', age: 23}
//cleaner way ?
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPassword = (({name, age}) => ({name, age}))(user);
//better way
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPassword = Object.keys(user)
.reduce((acc, key) => key === ‘password’ ?
acc : ({ …acc, [key]: user[key] }),
{}
);
//better way for collection of keys
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPasswordAndAge = Object.keys(user)
.reduce((acc, key) => ['password', 'age'].indexOf(key) > -1 ?
acc : ({ …acc, [key]: user[key] }),
{}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment