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]);
I've tried using Object.keys but that only prints the TOP key of JSON.parse(data[0]['content']['message'])['gtmstate'][36]
-
stackoverflow.com/questions/6268679/…KAngel7– KAngel72016年11月21日 07:35:45 +00:00Commented Nov 21, 2016 at 7:35
-
will your objects have only one key all the timeGeeky– Geeky2016年11月21日 07:36:28 +00:00Commented Nov 21, 2016 at 7:36
-
@Geeky - yes as far as I knowCMS– CMS2016年11月21日 07:50:31 +00:00Commented Nov 21, 2016 at 7:50
3 Answers 3
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
answered Nov 21, 2016 at 7:39
Geeky
7,5142 gold badges28 silver badges52 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
CMS
I want to dynamically get the value, not have to hard code it, the keys can change from my data source
CMS
Result: 0-------[object Object] 1-------[object Object] 2-------[object Object] 3-------[object Object]
Akshay
console.log arr and see to it its an array or object
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);
answered Nov 21, 2016 at 8:19
Rohìt Jíndal
27.3k16 gold badges80 silver badges133 bronze badges
Comments
lang-js