javascript 中的 Defaultdict 等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19127650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Defaultdict equivalent in javascript
提问by user462455
In python you can have a defaultdict(int) which stores int as values. And if you try to do a 'get' on a key which is not present in the dictionary you get zero as default value.
在 python 中,你可以有一个 defaultdict(int) 将 int 存储为值。如果您尝试对字典中不存在的键执行“获取”,则默认值为零。
Can you do the same in javascript/jquery
你能在 javascript/jquery 中做同样的事情吗
回答by Andy Carlson
You can build one using a JavaScript Proxy
您可以使用 JavaScript 构建一个 Proxy
var defaultDict = new Proxy({}, {
get: (target, name) => name in target ? target[name] : 0
})
This lets you use the same syntax as normal objects when accessing properties.
这使您可以在访问属性时使用与普通对象相同的语法。
defaultDict.a = 1
console.log(defaultDict.a) // 1
console.log(defaultDict.b) // 0
To clean it up a bit, you can wrap this in a constructor function, or perhaps use the class syntax.
为了稍微清理一下,您可以将其包装在构造函数中,或者使用类语法。
class DefaultDict {
constructor(defaultVal) {
return new Proxy({}, {
get: (target, name) => name in target ? target[name] : defaultVal
})
}
}
const counts = new DefaultDict(0)
console.log(counts.c) // 0
EDIT:The above implementation only works well with primitives. It should handle objects too by taking a constructor function for the default value. Here is an implementation that should work with primitives and constructor functions alike.
编辑:上述实现仅适用于原语。它也应该通过为默认值使用构造函数来处理对象。这是一个应该与原语和构造函数一起使用的实现。
class DefaultDict {
constructor(defaultInit) {
return new Proxy({}, {
get: (target, name) => name in target ?
target[name] :
(target[name] = typeof defaultInit === 'function' ?
new defaultInit().valueOf() :
defaultInit)
})
}
}
const counts = new DefaultDict(Number)
counts.c++
console.log(counts.c) // 1
const lists = new DefaultDict(Array)
lists.men.push('bob')
lists.women.push('alice')
console.log(lists.men) // ['bob']
console.log(lists.women) // ['alice']
console.log(lists.nonbinary) // []
回答by Brian Weidenbaum
Check out pycollections.js:
var collections = require('pycollections');
var dd = new collections.DefaultDict(function(){return 0});
console.log(dd.get('missing')); // 0
dd.setOneNewValue(987, function(currentValue) {
return currentValue + 1;
});
console.log(dd.items()); // [[987, 1], ['missing', 0]]
回答by DGS
I don't think there is the equivalent but you can always write your own. The equivalent of a dictionary in javascript would be an object so you can write it like so
我不认为有等价物,但你总是可以自己写。javascript 中字典的等价物将是一个对象,因此您可以像这样编写它
function defaultDict() {
this.get = function (key) {
if (this.hasOwnProperty(key)) {
return key;
} else {
return 0;
}
}
}
Then call it like so
然后像这样调用它
var myDict = new defaultDict();
myDict[1] = 2;
myDict.get(1);
回答by Nytshaed
To add to Andy Carlson's answer
添加到安迪卡尔森的答案
If you default dict an array, you'll get a toJSONfield in the resulting object. You can get rid of it by deconstructing to a new object.
如果你默认 dict 一个数组,你会在结果对象中得到一个toJSON字段。您可以通过解构为新对象来摆脱它。
const dd = new DefaultDict(Array);
//...populate the dict
return {...dd};