Multiple Key Names, Same Pair Value

Multiple key names, same pair value

Another approach is to do some postprocessing

function expand(obj) {
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i],
subkeys = key.split(/,\s?/),
target = obj[key];
delete obj[key];
subkeys.forEach(function(key) { obj[key] = target; })
}
return obj;
}

var holidays = expand({
"thanksgiving day, thanksgiving, t-day": {
someValue : "foo"
}
});

How to set the same value on multiple keys

Take the array of keys and map each to an entry of the key and the 5 value:

const keys = ['a', 'b', 'c'];const obj = {  ...Object.fromEntries(    keys.map(key => [key, 5])  ),  d: 6};
console.log(obj);

How to affect an object with multiple keys for the same value

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']]);

Javascript Map Object with multiple keys to one value

You can write your own helper to transform a less verbose input into the one which new Map() expects:

const mapReducer = (arr, [keys, val]) => [
...arr,
...(Array.isArray(keys)
? [...keys.map(key => [key, val])]
: [[keys, val]]
)
];

const mp = new Map([
[[2, 4], 'even'],
[[1, 3], 'odd'],
[0, 'meh...']
].reduce(mapReducer, []));

console.log([...mp.entries()])

How link multiple keys to the same value in the HashMap

If you want to associate a group of keys with the same object, it can be achieved by using a mutable object as a value.

For instance, you can make use of StringBuilder or implement a custom class. It'll be more performant and easier than an approach with implementing your own map which extends HashMap and is able to track these groups of keys and triggers a series of updates for each call of put(), replace() or remove().

Solution with a custom mutable Container can look like this:

HashMap<String, Container<Integer>> map = new HashMap<>();
Container<Integer> commonValue = new Container<>(0);
map.put("x", commonValue);
map.put("y", commonValue);

System.out.println("Value for 'x': " + map.get("x"));
System.out.println("Value for 'y': " + map.get("y"));

commonValue.setValue(10);

System.out.println("Value for 'x': " + map.get("x"));
System.out.println("Value for 'y': " + map.get("y"));

The Container class itself.

public class Container<T> {
private T value;

public Container(T value) {
this.value = value;
}

public T getValue() {
return value;
}

public void setValue(T value) {
this.value = value;
}

@Override
public String toString() {
return String.valueOf(value);
}
}

As I have already said, the alternative is to use a mutable class that is already provided by the JDK. The code is then almost the same:

HashMap<String, StringBuilder> map = new HashMap<>();
StringBuilder commonValue = new StringBuilder("0");
map.put("x", commonValue);
map.put("y", commonValue);

System.out.println("Value for 'x': " + map.get("x"));
System.out.println("Value for 'y': " + map.get("y"));

commonValue.replace(0, commonValue.length(), "10");

System.out.println("Value for 'x': " + map.get("x"));
System.out.println("Value for 'y': " + map.get("y"));

Output (by both versions)

Value for 'x': 0
Value for 'y': 0
Value for 'x': 10
Value for 'y': 10


Related Topics



Leave a reply



Submit