Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Exemplo:
- * const object = {a: 1, b: 2, c: 3};
- * const keys = ["a", "c"];
- * filter(object, keys);
- * //=> {a: 1, c: 3}
- */
- function filter(object = [], keys = []) {
- if (typeof object !== "object") throw new Error("must be an object");
- if (!Array.isArray(keys)) throw new Error("must be an array");
- if (!keys.every(key => typeof key === "string")) throw new Error("must be an array of strings");
- const newObject = {};
- keys.forEach(key => {
- const value = object[key];
- if (value) newObject[key] = value;
- });
- return newObject;
- }
Add Comment
Please, Sign In to add comment