Skip to content

Instantly share code, notes, and snippets.

@hahuaz
Created February 20, 2022 21:13
Show Gist options
  • Save hahuaz/15f8b78f91192aa2379b6ffc9fd0c3ea to your computer and use it in GitHub Desktop.
Save hahuaz/15f8b78f91192aa2379b6ffc9fd0c3ea to your computer and use it in GitHub Desktop.
/**
*
* @param {} obj object to filter
* @param {...any} allowedKeys proporties to keep
* @returns object with only allowed keys
*/
const filterObj = (obj, ...allowedKeys) => {
const newObj = {};
Object.keys(obj).forEach((key) => {
if (allowedKeys.includes(key)) {
newObj[key] = obj[key];
}
});
return newObj;
};
const obj1 = {
name: 'john',
email: '[email protected]',
password: 'test123',
accessType: 'user',
};
console.log(filterObj(obj1, 'name', 'email'));
/* usage of arrow function inside the method */
const birthdayParty = {
forWho: 'mia',
friends: ['jonas', 'andrew', 'lara'],
// function declaration with concise syntax. You shouldn't use arrow function to create method.
inviteFriends() {
console.log(`This party for ${this.forWho.toUpperCase()}.`);
/**
* In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever.
* Arrow function does not have its own bindings to 'this'. It uses enclosing scope's 'this'
*/
this.friends.forEach((friend) => {
console.log(`${friend}, are you coming for ${this.forWho}'s party?`);
});
},
};
birthdayParty.inviteFriends();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment