在jQuery中通过索引获取字典的键值 [英] Get key value of dictionary by index in jQuery

查看:324
本文介绍了在jQuery中通过索引获取字典的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript字典对象,它具有默认为 0 的预设键。然后,我需要通过 index 循环遍历该字典的元素,并使用该键的值来设置其值。以下是我的代码,使事情更容易理解:

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);
        }
    }

换句话说,假设我们正在处理C# ,我想在某个索引中获取 KeyValuePair ,然后访问其 Key Value 属性。

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.

任何建议?

推荐答案

对于对象 _map ,您应该使用 for .. in

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

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

这篇关于在jQuery中通过索引获取字典的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆