Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @function objectWithoutKeys
- * Rebuilds Object without banned keys. returns copy of object
- * @param {*} obj
- * @param {string[]} bannedKeys
- */
- function objectWithoutKeys(obj, ...bannedKeys) {
- const sanitizedKeys =
- Object.keys(obj).filter(k => bannedKeys.indexOf(k) === -1);
- let newObj = {};
- for (let i = 0; i < sanitizedKeys.length; i++) {
- newObj[sanitizedKeys[i]] = user[sanitizedKeys[i]];
- }
- return newObj;
- }
- // Usage...
- const user = {
- name: 'HELLO',
- age: 56,
- password: 'password',
- createdAt: new Date(),
- };
- console.log('user', user);
- console.log('userWithoutPassword', objectWithoutKeys(user, 'password'));
- console.log('userWithoutPasswordAndName', objectWithoutKeys(user, 'password', 'name'));
Add Comment
Please, Sign In to add comment