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]

3
  • stackoverflow.com/questions/6268679/… Commented Nov 21, 2016 at 7:35
  • will your objects have only one key all the time Commented Nov 21, 2016 at 7:36
  • @Geeky - yes as far as I know Commented Nov 21, 2016 at 7:50

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

I want to dynamically get the value, not have to hard code it, the keys can change from my data source
Result: 0-------[object Object] 1-------[object Object] 2-------[object Object] 3-------[object Object]
console.log arr and see to it its an array or object
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.