In my projects, I often prefer to organize my utility functions neatly, especially when they grow in number. For javascript filter object functionality, creating a namespace for your utilities helps maintain structure and prevent any potential naming conflicts.
const MyUtils = {
filterObject: function(obj, callback) {
const result = {};
for (let key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (callback(obj[key], key, obj)) {
result[key] = obj[key];
}
}
}
return result;
}
};
// Example
const data = { name: "Alex", role: "admin", age: 25 };
const adminOnly = MyUtils.filterObject(data, (val, key) => key === "role");
console.log(adminOnly); // { role: "admin" }
By using a custom namespace like MyUtils, you ensure that your code stays modular and reusable, especially in larger projects. It’s a nice way to keep everything tidy and avoid conflicts while adding useful functions like javascript filter object