Manoj Singh Manoj Singh - 2 months ago 5
JSON Question

How get array value by key in javascript

I am getting following javascript array and i need to get value of that array by key. Any suggestion will be appreciated.

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


I have created following code -

'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}]
}

}


I am getting dynamic value of payment_tariff's keys

For example, I need to get value of key 5, Where key is some value that will be further processed.

Answer

I'm still not 100% sure about what you want as a result, but I think given payment_tariff and two values (4142 and 2), you want an array of keys from the array of objects that those values identify.

I updated the demo to return an array of keys and values as I suspect you might want that. If this is not the result you seek then please specify exactly what the result is you do seek.

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

Comments