I am able to read a json array from php, i want to display the key name of the items aswell. This might not make sense now but please check below code.
My json data
我能够从php读取一个json数组,我想显示项目的关键名称。这可能现在没有意义,但请检查下面的代码。我的json数据
{ "A":{"Africa":"201455632", "Asia":"5145000"},
"B":{"Brasil":"68455222"},
"C":{"China":"14546787"}
}
My js code i am able to dislplay the key-val pair
我的js代码我可以打开key-val对
$.each(data, function() {
console.log('---')
$.each(this, function(k, v) {
console.log(k, v)
});
});
Its displays like this
它的显示是这样的
---
Africa 201455632
Asia 5145000
---
Brasil 68455222
---
China 14546787
My Problem is, i want to display it like this, with their key name aswell, what can i replace console.log('---') with
我的问题是,我希望用它们的关键名称显示它,还有什么可以替换console.log('---')
A
Africa 201455632
Asia 5145000
B
Brasil 68455222
C
China 14546787
2 个解决方案
#1
Use the parameters to the callback function (just like you did with the inner loop).
将参数用于回调函数(就像使用内循环一样)。
$.each(data, function(key, value) {
console.log(key);
$.each(value, function(k, v) {
console.log(' ' + k, v);
});
});
#2
$.each(data, function(key, value) {
console.log(key)
$.each(value, function(k, v) {
console.log(k, v)
});
});
Hope its help.
希望它的帮助。
#1
Use the parameters to the callback function (just like you did with the inner loop).
将参数用于回调函数(就像使用内循环一样)。
$.each(data, function(key, value) {
console.log(key);
$.each(value, function(k, v) {
console.log(' ' + k, v);
});
});
#2
$.each(data, function(key, value) {
console.log(key)
$.each(value, function(k, v) {
console.log(k, v)
});
});
Hope its help.
希望它的帮助。