如何在javascript中通过键获取数组值

时间:2016-09-17 19:08:14

标签: javascript json

我正在关注 javascript 数组,我需要按键获取该数组的值。任何建议将不胜感激。

[Object { 5=7224}, Object { 10=7225}, Object { 25=7226}, Object { 50=7227}] 

我创建了以下代码 -

'payment_tariff': {
    4142: 
        {  
            1: [{1: 7223}],
            2: [{5: 7224}, {10: 7225}, {25: 7226}, {50: 7227}],
            3: [{10: 7228}, {20: 7229}, {50: 7230}, {100: 7231}],
            4: [{25: 7232}, {50: 7233}, {100: 7234}, {250: 7235}],
            5: [{25: 7236}, {50: 7237}, {100: 7238}, {250: 7239}]
        }
    ,
    4130: 
        {
            1: [{1: 7132}],
            2: [{5: 7133}, {10: 7134}, {25: 7135}, {50: 7136}],
            3: [{10: 7137}, {25: 7138}, {50: 7139}, {100: 7140}],
            4: [{25: 7141}, {50: 7142}, {100: 7143}, {250: 7144}],
            5: [{25: 7145}, {50: 7146}, {100: 7147}, {250: 7148}]
        }
    ,
    4133: 
        {
            1: [{1: 7166}],
            2: [{5: 7167}, {10: 7168}, {25: 7169}, {50: 7170}],
            3: [{10: 7171}, {25: 7172}, {50: 7173}, {100: 7173}],
            4: [{25: 7174}, {50: 7175}, {100: 7176}, {250: 7177}],
            5: [{25: 7178}, {50: 7179}, {100: 7180}, {250: 7181}]
        }
    ,
    4134: 
        {
            1: [{1: 7188}],
            2: [{5: 7189}, {10: 7190}, {25: 7191}, {50: 7192}],
            3: [{10: 7193}, {25: 7194}, {50: 7195}, {100: 7298}],
            4: [{25: 7197}, {50: 7198}, {100: 7199}, {250: 7200}],
            5: [{25: 7201}, {50: 7202}, {100: 7203}, {250: 7204}]
        }
    ,
    4135: 
        {
            1: [{1: 7206}],
            2: [{5: 7207}, {10: 7208}, {25: 7209}, {50: 7210}],
            3: [{10: 7211}, {25: 7212}, {50: 7213}, {100: 7214}],
            4: [{25: 7215}, {50: 7216}, {100: 7217}, {250: 7218}],
            5: [{25: 7219}, {50: 7220}, {100: 7221}, {250: 7222}]
        }

}

我正在获得payment_tariff键的动态价值

例如,我需要获取键5的值,其中键是一些将被进一步处理的值。

2 个答案:

答案 0 :(得分:0)

您可以获取一个包含对象的字段(键)列表的数组,如下所示:

var keys = Object.keys(payment_tariff);
for(var i in keys) {
    var key_name = keys[i];
    console.log(key_name + " = " + payment_tariff[key_name]);
}

OBS: payment_tariff 是变量/对象/数组的名称;

更多信息:Mozilla Documentation

答案 1 :(得分:0)

我仍然不能100%确定你想要的结果,但我认为给出 payment_tariff 和两个值(4142和2),你需要一个来自这些值标识的对象数组。

我更新了演示以返回一系列键和值,因为我怀疑你可能想要它。如果这不是您所寻求的结果,请指明完全您要搜索的结果。



var data = {
  'payment_tariff': {
    4142: {
      1: [{1: 7223}],
      2: [{5: 7224}, {10: 7225}, {25: 7226}, {50: 7227}],
      3: [{10: 7228}, {20: 7229}, {50: 7230}, {100: 7231}],
      4: [{25: 7232}, {50: 7233}, {100: 7234}, {250: 7235}],
      5: [{25: 7236}, {50: 7237}, {100: 7238}, {250: 7239}]
    },
    4130: {
      1: [{1: 7132}],
      2: [{5: 7133}, {10: 7134}, {25: 7135}, {50: 7136}],
      3: [{10: 7137}, {25: 7138}, {50: 7139}, {100: 7140}],
      4: [{25: 7141}, {50: 7142}, {100: 7143}, {250: 7144}],
      5: [{25: 7145}, {50: 7146}, {100: 7147}, {250: 7148}]
    },
    4133: {
      1: [{1: 7166}],
      2: [{5: 7167}, {10: 7168}, {25: 7169}, {50: 7170}],
      3: [{10: 7171}, {25: 7172}, {50: 7173}, {100: 7173}],
      4: [{25: 7174}, {50: 7175}, {100: 7176}, {250: 7177}],
      5: [{25: 7178}, {50: 7179}, {100: 7180}, {250: 7181}]
    },
    4134: {
      1: [{1: 7188}],
      2: [{5: 7189}, {10: 7190}, {25: 7191}, {50: 7192}],
      3: [{10: 7193}, {25: 7194}, {50: 7195}, {100: 7298}],
      4: [{25: 7197}, {50: 7198}, {100: 7199}, {250: 7200}],
      5: [{25: 7201}, {50: 7202}, {100: 7203}, {250: 7204}]
    },
    4135: {
      1: [{1: 7206}],
      2: [{5: 7207}, {10: 7208}, {25: 7209}, {50: 7210}],
      3: [{10: 7211}, {25: 7212}, {50: 7213}, {100: 7214}],
      4: [{25: 7215}, {50: 7216}, {100: 7217}, {250: 7218}],
      5: [{25: 7219}, {50: 7220}, {100: 7221}, {250: 7222}]
    }
  }
};

var result = data.payment_tariff[4142][2].map(function(item){
  var key = Object.keys(item)[0];
  return { "key": key, "value": item[key] };
});

console.log(result);