I've noticed a common pattern that I think could stand to be simplified.
const labels = {
type1: 'Type 1',
type2: 'Type 2',
type3: 'Type 3',
};
const descriptions = {
type1: 'Type 1 Description',
type2: 'Type 2 Description',
type3: 'Type 3 Description',
};
// assume that this `type` variable updates reactively
let type = 'type3';
const [label, description] = [labels, descriptions].map(obj => obj[type]);
I found myself doing this quite a bit, where I had to access a certain key from each of the objects in an array. What if we had something like keyMap(key)
method for arrays, that allowed you to get the value for that key in several objects at once? It's not hard as it is, but it would certainly streanline the process a bit. For example:
const [label, description] = [labels, descriptions].keyMap(type);
// or
[[1, 2, 3], [4, 5, 6], [7, 8, 9]].keyMap(1) // [2, 5, 8]
The implementation could be something like this:
Array.prototype.keyMap = function(key) {
return this.map(obj => obj[key]);
}
So with this, you'd get either the value at the key, or undefined
if the key isn't there.