javascript 将元素添加到javascript“字典”数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20392390/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:23:51  来源:igfitidea点击:

Add element into javascript "dictionary" array

javascriptjqueryarrays

提问by Edi Wang

I got a dict object structure like this:

我得到了一个像这样的 dict 对象结构:

var dict = {
    11: 0,
    12: 0,
    13: 0
    ...
};

I want to do this:

我想做这个:

for (var i = 11; i < 42; i++) {
    dict.Add(i, 0);
}

But I do know "Add()" is not a function defined on the dict object.

但我知道“Add()”不是在 dict 对象上定义的函数。

So, how can I do it insead of manully write 11~42 line by line?

那么,除了手动写 11~42 行,我该怎么做呢?

回答by Matt

Is this what you are looking for?

这是你想要的?

for (var i = 11; i < 42; i++) {
    dict[i] = 0;
}

If you want to include 42per your question, then change the <to a <=.

如果你想包括42根据您的问题,然后更改<<=