Defaultdict equivalent in javascript

前端 未结 5 1206
小蘑菇
小蘑菇 2020-12-29 22:32

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

5条回答
  •  盖世英雄少女心
    2020-12-29 23:24

    To add to Andy Carlson's answer

    If you default dict an array, you'll get a toJSON field in the resulting object. You can get rid of it by deconstructing to a new object.

    const dd = new DefaultDict(Array);
    //...populate the dict
    return {...dd};
    

提交回复
热议问题