Guest User

Untitled

a guest
Feb 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. /**
  2. * Exemplo:
  3. * const object = {a: 1, b: 2, c: 3};
  4. * const keys = ["a", "c"];
  5. * filter(object, keys);
  6. * //=> {a: 1, c: 3}
  7. */
  8. function filter(object = [], keys = []) {
  9. if (typeof object !== "object") throw new Error("must be an object");
  10.  
  11. if (!Array.isArray(keys)) throw new Error("must be an array");
  12.  
  13. if (!keys.every(key => typeof key === "string")) throw new Error("must be an array of strings");
  14.  
  15. const newObject = {};
  16.  
  17. keys.forEach(key => {
  18. const value = object[key];
  19.  
  20. if (value) newObject[key] = value;
  21. });
  22.  
  23. return newObject;
  24. }
Add Comment
Please, Sign In to add comment