Recently, I've started delving into the world of es6 Map due to its unique features, but I have some reservations regarding pure operations.
For example, when removing properties from objects, I usually use the following function:
function cloneOmit( obj, props ) {
const keys = !Array.isArray(props)
? Object.keys(obj).filter(k => k !== props)
: Object.keys(obj).filter(k => !props.includes(k));
return keys.reduce(
(clone, key) => {
return { ...clone, [key]: obj[key] };
},
{}
);
}
I adapted this function to work with Maps as well:
function cloneOmitMap( map, keys ) {
const oldKeys = Array.from(map.keys());
const newKeys = !Array.isArray(keys)
? oldKeys.filter(k => k !== keys)
: oldKeys.filter(k => !keys.includes(k));
return newKeys.reduce((newMap, key) => {
return newMap.set(key, map.get(key));
}, new Map());
}
This solution works, but I'm unsure about its performance and effectiveness. While I appreciate Maps for their iteration capabilities (using Object.keys()
repetitively can be cumbersome), maintaining order insertion, and allowing any value as a key, they don't seem as conducive to pure operations as plain Objects are.
For example, adding a property to an object purely can be done simply like this:
const object = { foo: 'bar' }
const newObject= { ...object, fooFoo: 'barBar' }
I'm curious if there are any lesser-known Map operations that could facilitate working with them in a more pure manner, or perhaps a utility library that might help. Any guidance would be greatly appreciated!