我有一个接受配置并返回值的逻辑。但是,我想从缓存中检索相同配置的进一步尝试。密钥的顺序可以在配置中改变,但必须视为相同的配置。
评论解释了我试图达到的目标。我希望只有两个开口,因为在示例代码中只有两个不同的配置。只有第一次和第二次尝试
open()
按预期进行,因为同一对象作为键传递到映射。
如果我试图将键保留为json字符串,那么键的顺序可能会有问题。
这是我到目前为止所做的努力。我欣赏任何想法或其他解决方案。
var m = new Map();
function opening(config) {
// ...
return "foo";
}
function open(config = {}) {
if (!m.has(config)) {
m.set(config, opening(config));
console.log("open and set: ", config);
} else {
console.log("get from the map: ", config);
}
return m.get(config);
}
var c1 = { a: 1, b: 2 };
open(c1); // open and set [OK]
open(c1); // get from the map [OK]
open({ a: 1, b: 2 }); // get from the map
open({ b: 2, a: 1 }); // get from the map, even in different orders
open({ a: 1, b: 2, c: 3 }); // open and set
open({ a: 1, c: 3, b: 2 }); // get from the map