Guest User

Untitled

a guest
Sep 2nd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. /**
  2. * @function objectWithoutKeys
  3. * Rebuilds Object without banned keys. returns copy of object
  4. * @param {*} obj
  5. * @param {string[]} bannedKeys
  6. */
  7. function objectWithoutKeys(obj, ...bannedKeys) {
  8. const sanitizedKeys =
  9. Object.keys(obj).filter(k => bannedKeys.indexOf(k) === -1);
  10.  
  11. let newObj = {};
  12. for (let i = 0; i < sanitizedKeys.length; i++) {
  13. newObj[sanitizedKeys[i]] = user[sanitizedKeys[i]];
  14. }
  15. return newObj;
  16. }
  17.  
  18. // Usage...
  19.  
  20. const user = {
  21. name: 'HELLO',
  22. age: 56,
  23. password: 'password',
  24. createdAt: new Date(),
  25. };
  26.  
  27. console.log('user', user);
  28. console.log('userWithoutPassword', objectWithoutKeys(user, 'password'));
  29. console.log('userWithoutPasswordAndName', objectWithoutKeys(user, 'password', 'name'));
Add Comment
Please, Sign In to add comment