Javascript:如果我不知道对象的关键字,我如何访问嵌套对象中的值? [英] Javascript: How do I access values in nested objects if I don’t know the object’s key?

查看:161
本文介绍了Javascript:如果我不知道对象的关键字,我如何访问嵌套对象中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个从JSON解析的Javascript对象,它嵌套了三个深度,我不知道中间关键字的关键,我如何访问它和它的内容?

我正在使用的实际数据来自Github API。对于这个例子,我想要所有的要点的文件名。

  [
{
url :https://api.github.com/gists/11164200,
forks_url:https://api.github.com/gists/11164200/forks,
commits_url :https://api.github.com/gists/11164200/commits,
id:11164200,
git_pull_url:https://gist.github.com /11164200.git,
git_push_url:https://gist.github.com/11164200.git,
html_url:https://gist.github.com/11164200 ,
files:{
testing.md:{
filename:testing.md,
type:text / plain,
language:Markdown,
raw_url:https://gist.githubusercontent.com/omphalosskeptic/11164200/raw/3582779a4925ea514382cedb7d077d00c231f3eb/testing.md,
size :4254
}
},// [...继续]

我的JavaScript技能是基本的。通常我可以通过足够的研究找到我期待的内容,但目前还没有。最初我预计它会是这样的: responseObj [0] .files [0] .filename



如果可能的话,我想保留这个简单的Javascript。



谢谢!

解决方案

根据您发布的示例,文件属性不是数组,不能被索引器访问。在这种情况下,您可以使用 for-in 循环而不是常规来循环循环。

  for(var p in responseObj [0] .files){
if(responseObj [0] .files.hasOwnProperty(p) ){
p; // p是你未知的属性名称
responseObj [0] .files [p]; //是可以用来访问
//它自己的属性(文件名,类型等)的对象
}
}

hasOwnProperty 检查将跳过自动成员,如 toString 并且只返回在对象上手动定义的那些。


If I have a Javascript object, parsed from JSON, that’s nested three deep, and I don’t know the key for the middle one, how to I access it and its contents?

The actual data I’m working with is from the Github API. For this example, I want the file names for all my gists.

[
  {
    "url": "https://api.github.com/gists/11164200",
    "forks_url": "https://api.github.com/gists/11164200/forks",
    "commits_url": "https://api.github.com/gists/11164200/commits",
    "id": "11164200",
    "git_pull_url": "https://gist.github.com/11164200.git",
    "git_push_url": "https://gist.github.com/11164200.git",
    "html_url": "https://gist.github.com/11164200",
    "files": {
      "testing.md": {
        "filename": "testing.md",
        "type": "text/plain",
        "language": "Markdown",
        "raw_url": "https://gist.githubusercontent.com/omphalosskeptic/11164200/raw/3582779a4925ea514382cedb7d077d00c231f3eb/testing.md",
        "size": 4254
      }
    }, // [ ... continues]

My Javascript skills are rudimentary. Normally I can find what I’m looking for with enough research but not this time. Originally I expected it would be something like: responseObj[0].files[0].filename.

If possible I’d like to keep this plain Javascript.

Thanks!

解决方案

Based on the sample you posted, the files property is not an array, so can't be accessed by an indexer. This is a case where you would use a for-in loop rather than a regular for loop.

for(var p in responseObj[0].files) {                 
  if ( responseObj[0].files.hasOwnProperty (p) ) {   
    p; // p is your unknown property name
    responseObj[0].files[p]; // is the object which you can use to access 
                             // its own properties (filename, type, etc)           
  }                                                                                                           
}

The hasOwnProperty check will skip the automatic members like toString and only return those manually defined on the object.

这篇关于Javascript:如果我不知道对象的关键字,我如何访问嵌套对象中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆