I have some rules lookups that are based on state.. I am trying to think of a way to make accessing these easy without setting up some sort of intermediate step.
I'm imagining something like:
rules = {
['AK','FL','NY']: {a: 278},
['CA','LA','TX']: {a: 422}
}
console.log(rules['AK']); //278
console.log(rules['TX']); //422
I know that's not possible.. but wondering what the simplest way to achieve something like that would be
No, JavaScript objects cannot have duplicate keys. The keys must all be unique.
An object can have two same keys or two same values.
Objects are Variables JavaScript variables can also contain many values. Objects are variables too. But objects can contain many values.
Take a page from MySQL's foreign key :
var states = {
'AK' : 1,
'FL' : 1,
'NY' : 1,
'CA' : 2,
'LA' : 2,
'TX' : 2
}
var rules = {
1 : 278,
2 : 422
}
Then you can reference the rules like so :
console.log(rules[states['AK']]);
Affect Keys with same rule :
var rule1 = {a : 278};
var rule2 = {a : 422};
var rules = {
'AK' : rule1,
'FL' : rule1,
'NY' : rule1,
'CA' : rule2,
'TX' : rule2
};
console.log(rules['AK']); // {a:278}
console.log(rules['AK'].a); // 278
https://jsfiddle.net/sb5az0v5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With