Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add element into javascript "dictionary" array

I got a dict object structure like this:

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.

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

like image 214
Edi Wang Avatar asked Jul 19 '26 04:07

Edi Wang


1 Answers

Is this what you are looking for?

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

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

like image 173
Matt Avatar answered Jul 21 '26 17:07

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!