Exercise 6: Omit Keys from Object
Problem Statement
Function `omit(obj, keys)` banao jo object se specified keys remove kare.
Example: omit({a: 1, b: 2, c: 3}, ["a", "c"]) => {b: 2}
Sample Output:
omit({a: 1, b: 2, c: 3}, ["a", "c"]) => {b: 2}
omit({x: 1, y: 2}, ["x"]) => {y: 2}Solution
const omit = (obj, keys) => {
const keySet = new Set(keys);
return Object.keys(obj).reduce((acc, k) => {
if (!keySet.has(k)) acc[k] = obj[k];
return acc;
}, {});
};Explanation
Overall Goal:
- Object se specified keys remove karna.
Real world:
- Data sanitization: sensitive fields remove.
- Object transformation: key filtering.