const pick = (obj = {}, keys = []) => {
const result = {};
for (const key in obj) {
if (keys.includes(key)) {
result[key] = obj[key];
}
}
return result;
};
const obj = {
name: "Mehul",
age: 27,
email: "codedrops.tech@gmail.com",
website: "codedrops.tech",
};
const keysToInclude = ["name", "age"];
console.log(pick(obj, keysToInclude)); // { name: 'Mehul', age: 27 }
Thanks for reading đź’™
Follow @codedrops.tech for more.
Instagram â—Ź Twitter â—Ź Facebook
Micro-Learning â—Ź Web Development â—Ź Javascript â—Ź MERN stack
codedrops.tech
Projects
File Ops - A VS Code extension to easily tag/alias files & quick switch between files
Top comments (0)