0

How do I access the values "CFTO-A","CFTO-B", "CFTO-C", "CFTO-D" in this object.

The object comes from this:

 console.log(JSON.parse(data[0]['content']['message'])['gtmstate'][36]);

Object Details

I've tried using Object.keys but that only prints the TOP key of JSON.parse(data[0]['content']['message'])['gtmstate'][36]

CMS
  • 285
  • 1
  • 4
  • 10

3 Answers3

1

Try this snippet

var arr = [{
  "CFTO-A": 10
}, {
  "CFTO-B": 20
}, {
  "CFTO-C": 30
}, {
  "CFTO-D": 40
}];
arr.forEach(function(item) {
  Object.keys(item).forEach(function(key) {
    alert(key);
  })
})

Hope it helps

Geeky
  • 7,420
  • 2
  • 24
  • 50
0
 var arr = JSON.parse(data[0]['content']['message'])['gtmstate'][36]
$.each(arr,function(i,data){
$.each(data,function(j,kal){
console.log(j+"-------"+kal)
})
})
Akshay
  • 815
  • 7
  • 16
  • I want to dynamically get the value, not have to hard code it, the keys can change from my data source – CMS Nov 21 '16 at 07:32
  • Result: 0-------[object Object] 1-------[object Object] 2-------[object Object] 3-------[object Object] – CMS Nov 21 '16 at 07:44
  • console.log arr and see to it its an array or object – Akshay Nov 21 '16 at 07:55
0

Use JavaScript Array map() method.

Working Demo :

var jsonObj = [{
  "CFTO-A": 10
}, {
  "CFTO-B": 20
}, {
  "CFTO-C": 30
}, {
  "CFTO-D": 40
}];

var resArray = [];
jsonObj.map(function(item) {
  Object.keys(item).map(function(data) {
    resArray.push(item[data]);
  })
});

console.log(resArray);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123