javascript json 获取键名作为文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4222596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:43:44  来源:igfitidea点击:

json get key names as text?

javascriptjson

提问by Hailwood

I would like to find out the key of a json value.

我想找出一个 json 值的键。

I am doing

我在做

for(var i in rows){
    for(var j in rows[i]){
        console.log('value: ', rows[i][j]);
    }
}

However I want to be able to do something like

但是我希望能够做类似的事情

for(var i in rows){
    for(var j in rows[i]){
        console.log('key: rows[i][j].key', 'value: ',rows[i][j]);
    }
}

or whatever so I can access the key.

或任何我可以访问密钥的东西。

回答by DanneManne

When you use the syntax for(var i in rows) i is actually the key so you should be able to do:

当您使用 for(var i in rows) 的语法时,我实际上是关键,因此您应该能够执行以下操作:

console.log('key: ',j, ' value: ',rows[i][j]);