Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
131
rated 0 times [  135] [ 4]  / answers: 1 / hits: 43507  / 12 Years ago, tue, june 5, 2012, 12:00:00

I have a javascript dictionary object which has a pre-set keys that are defaulted to 0. Then I need to loop through the elements of this dictionary by index and use the value of the key to set its value. Below is my code to make things easier to understand:



var _map = {
'severity-normal': 0,
'severity-minimal': 0,
'severity-moderate': 0,
'severity-severe': 0,
'severity-highly-severe': 0
};

mapSeverities: function () {
for (var i = 0; i < _map.length; i++) {
//get the key value, ex: severity-normal, by index (which would by i)
var key = //retrieved key value
_map[key] = this.data(key);
}
}


In other words, suppose we're dealing with C#, I want to get the KeyValuePair at a certain index, then access its Key and Value properties.



Any suggestions?


More From » jquery

 Answers
-4

For object _map, you should use for .. in.



for (var key in _map) {
_map[key] = this.data[key];
}

[#85138] Monday, June 4, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;