Last active
February 13, 2018 16:50
-
-
Save wilomgfx/62cc2cf3d565f5fbedc902f34c58206e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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