Title basically, I've just begun exploring es6 Map because of it's unique properties, and I'm a bit concerned about pure operations.
For example often remove properties from objects I work with using this:
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 rewrote this to work with Maps:
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());
}
Which is fine, but I'm not sure if it's performant or even the best way. I am attracted to Maps for their iteration capabilities (Object.keys()
is cumbersome and ugly to constantly call), their adherence to order insertion, and especially their allowance of any value as a key but they do not seem nearly as conducive to pure operations as plain Objects are.
For instance, if I want to add a property to an object purely:
const object = { foo: 'bar' }
const newObject= { ...object, fooFoo: 'barBar' }
I'm wondering if there are some Map operations I'm not wary of that might help facilitate working with them purely, or maybe even a small utility library. Any help is appreciated!