乐闻世界logo
搜索文章和话题

How can I create a two-way mapping in JavaScript, or some other way to swap out values?

1个答案

1

An effective approach to creating a bidirectional mapping in JavaScript is to use an object to maintain mappings from keys to values and from values to keys. This can be implemented by creating a specialized structure that ensures both mappings are updated simultaneously when adding or modifying a mapping.

Method One: Implementing Bidirectional Mapping Using an Object

Here is an example demonstrating how to implement such a bidirectional mapping:

javascript
class BiMap { constructor() { this.keyToValue = {}; this.valueToKey = {}; } set(key, value) { // Remove the existing key-value mapping if (this.keyToValue[key]) { delete this.valueToKey[this.keyToValue[key]]; } if (this.valueToKey[value]) { delete this.keyToValue[this.valueToKey[value]]; } // Set the new mapping this.keyToValue[key] = value; this.valueToKey[value] = key; } getKey(value) { return this.valueToKey[value]; } getValue(key) { return this.keyToValue[key]; } removeByKey(key) { const value = this.keyToValue[key]; if (value) { delete this.valueToKey[value]; delete this.keyToValue[key]; } } removeByValue(value) { const key = this.valueToKey[value]; if (key) { delete this.keyToValue[key]; delete this.valueToKey[value]; } } } // Usage example const biMap = new BiMap(); biMap.set('a', 1); biMap.set('b', 2); console.log(biMap.getKey(1)); // Output: 'a' console.log(biMap.getValue('b')); // Output: 2 biMap.set('a', 3); // Update 'a' value console.log(biMap.getKey(1)); // Output: undefined console.log(biMap.getKey(3)); // Output: 'a'

Method Two: Using Map Objects

If you prefer a more flexible or built-in data structure, consider using two Map objects to store the key-to-value and value-to-key mappings separately.

javascript
class BiDirectionalMap { constructor() { this.keyToValue = new Map(); this.valueToKey = new Map(); } set(key, value) { // Remove the existing key-value mapping if (this.keyToValue.has(key)) { this.valueToKey.delete(this.keyToValue.get(key)); } if (this.valueToKey.has(value)) { this.keyToValue.delete(this.valueToKey.get(value)); } // Set the new mapping this.keyToValue.set(key, value); this.valueToKey.set(value, key); } getKey(value) { return this.valueToKey.get(value); } getValue(key) { return this.keyToValue.get(key); } removeByKey(key) { const value = this.keyToValue.get(key); if (value !== undefined) { this.valueToKey.delete(value); this.keyToValue.delete(key); } } removeByValue(value) { const key = this.valueToKey.get(value); if (key !== undefined) { this.keyToValue.delete(key); this.valueToKey.delete(value); } } } // Usage example const map = new BiDirectionalMap(); map.set('x', 100); map.set('y', 200); console.log(map.getKey(100)); // Output: 'x' console.log(map.getValue('y')); // Output: 200

Both methods offer flexible bidirectional mapping functionality, ensuring consistency in the mappings during addition, retrieval, and deletion of elements. This is particularly useful for scenarios requiring fast lookup of keys and values.

2024年6月29日 12:07 回复

你的答案