Reputation: 2028
I have this Object below, and as you can see, "valid" and "pending" are using the same value, is there a way to put this in one line rather than duplicating "custom-info" ?
Map = {
'failed': 'custom-error',
'pending': 'custom-info',
'valid': 'custom-info'
};
Upvotes: 3
Views: 533
Reputation: 558
Map = {
'failed': 'custom-error'
};
Map.pending = Map.valid = 'custom-info';
Upvotes: 1
Reputation: 320
This will get you a shorthand, but remember that strings, being primitives, are passed as copies, and this may not behave as you need.
Enum = {
'error': 'custom-error',
'info': 'custom-info'
};
Map = {
'failed': Enum.error,
'pending': Enum.info,
'valid': Enum.info
};
If you need actual references to objects, I suggest:
Enum = function(type) {
var keys = {
error : { text: "custom-error" },
info : { text: "custom-info" }
};
return keys[type];
};
Map = {
'failed': Enum('error'),
'pending': Enum('info'),
'valid': Enum('info')
};
Upvotes: 1
Reputation: 3733
You can assign string value to constant and set property value using constant.
const CUSTOMER_INFO = 'custom-info';
Map = {
'failed': 'custom-error',
'pending': CUSTOMER_INFO,
'valid': CUSTOMER_INFO
};
This helps only if setting property value many times and used somewhere else also otherwise setting string value in object is fine.
Upvotes: 2