简体   繁体   English

如何使用javascript访问位于对象数组中的对象的属性?

[英]How to access properties of an object that is in an array of object using javascript?

The following is ONE ELEMENT of a responseText from an ajax call: 以下是来自ajax调用的responseText的一个元素:

[{
    "hitcount": {
        "0": "3351"
    },
    "page": {
        "0": "1"
    },
    "title": {
        "0": "Hypoxia upregulates Rab11-family interacting protein 4 through HIF-1\u03b1 to promote the metastasis of hepatocellular carcinoma."
    },
    "authorString": {
        "0": "Hu F, Deng X, Yang X, Jin H, Gu D, Lv X, Wang C, Zhang Y, Huo X, Shen Q, Luo Q, Zhao F, Ge T, Zhao F, Chu W, Shu H, Yao M, Fan J, Qin W."
    },
    "pubYear": {
        "0": "2015"
    },
    "journalVolume": "",
    "issue": "",
    "pageInfo": "",
    "pmid": {
        "0": "25745995"
    },
    "abstract_": {
        "0": "Hypoxic microenvironment is a powerful driving force for the invasion and metastasis of hepatocellular carcinoma (HCC). Hypoxia-inducible factor 1\u03b1 (HIF-1\u03b1), as a crucial regulator of transcriptional responses to hypoxia, induces the expression of multiple target genes involved in different steps of HCC metastatic process. It is critical to find target genes associated with metastasis under hypoxia for shedding new light on molecular mechanism of HCC metastasis. In this study, we uncovered that hypoxia could induce the upregulation of Rab11-family interacting protein 4 (Rab11-FIP4) and activation of Rab11-FIP4 promoter by HIF-1\u03b1. The overexpression of Rab11-FIP4 significantly enhanced the mobility and invasiveness of HCC cells in vitro, also contributed to distant lung metastasis in vivo, whereas silencing of Rab11-FIP4 decreased the ability of migration and invasion in HCC cells in vitro and suppressed lung metastasis in vivo. Rab11-FIP4 facilitated HCC metastasis through the phosphorylation of PRAS40, which was regulated by mTOR. Furthermore, the expression level of Rab11-FIP4 was significantly increased in HCC tissues and high expression of Rab11-FIP4 was closely correlated with vascular invasion and poor prognosis in HCC patients. A markedly positive correlation between the expression of Rab11-FIP4 and HIF-1\u03b1 was observed in HCC tissues and combination of Rab11-FIP4 and HIF-1\u03b1 was a more valuable predictor of poor prognosis for HCC patients. In conclusion, Rab11-FIP4 is a target gene of HIF-1\u03b1 and has a pro-metastatic role in HCC, suggesting that Rab11-FIP4 may be a promising candidate target for HCC treatment.Oncogene advance online publication, 9 March 2015; doi:10.1038\/onc.2015.49."
    },
    "journalTitle": {
        "0": "Oncogene"
    },
    "pdfURL": "",
    "arrayOfPersons": [{
        "firstname": "dean",
        "lastname": "wood",
        "avatar_path": null,
        "username": "deanwood"
    }, {
        "firstname": "dean",
        "lastname": "wood",
        "avatar_path": null,
        "username": "deanwood"
    }, {
        "firstname": "Anne",
        "lastname": "Smith",
        "avatar_path": null,
        "username": "annesmith"
    }]
}]

I convert the string to a javascript object: 我将字符串转换为javascript对象:

 var json_obj = JSON.parse(xmlhttp.responseText);

I am trying to access the properties of the array of objects from this element (located at the end of the element) but have not been successful. 我正在尝试从此元素(位于元素的末尾)访问对象数组的属性,但未成功。 I have tried the following without success: 我尝试了以下操作,但未成功:

 json_obj[i].arrayOfPersons[i].firstname;

Please can somone advise? 请您能指教吗?

First of all: fix you JSON, it's missing an ] at the end. 首先:修复您的JSON,结尾缺少一个]。 Other than that you probably want to iterate through all possible firstNames. 除此之外,您可能还想遍历所有可能的名字。 Try this: 尝试这个:

for (var i = 0; i < json_obj.length; i++) {
    for (var j = 0; j < json_obj[i].arrayOfPersons.length; j++) {
        var result = json_obj[i].arrayOfPersons[j];
        console.log(result.firstname);
    }
}

Get the last person of arrayOfPersons : 获取arrayOfPersons的最后一个人:

  var json = '[{"hitcount":{"0":"3351"},"page":{"0":"1"},"title":{"0":"Hypoxia upregulates Rab11-family interacting protein 4 through HIF-1\α to promote the metastasis of hepatocellular carcinoma."},"authorString":{"0":"Hu F, Deng X, Yang X, Jin H, Gu D, Lv X, Wang C, Zhang Y, Huo X, Shen Q, Luo Q, Zhao F, Ge T, Zhao F, Chu W, Shu H, Yao M, Fan J, Qin W."},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":"","pmid":{"0":"25745995"},"abstract_":{"0":"Hypoxic microenvironment is a powerful driving force for the invasion and metastasis of hepatocellular carcinoma (HCC). Hypoxia-inducible factor 1\α (HIF-1\α), as a crucial regulator of transcriptional responses to hypoxia, induces the expression of multiple target genes involved in different steps of HCC metastatic process. It is critical to find target genes associated with metastasis under hypoxia for shedding new light on molecular mechanism of HCC metastasis. In this study, we uncovered that hypoxia could induce the upregulation of Rab11-family interacting protein 4 (Rab11-FIP4) and activation of Rab11-FIP4 promoter by HIF-1\α. The overexpression of Rab11-FIP4 significantly enhanced the mobility and invasiveness of HCC cells in vitro, also contributed to distant lung metastasis in vivo, whereas silencing of Rab11-FIP4 decreased the ability of migration and invasion in HCC cells in vitro and suppressed lung metastasis in vivo. Rab11-FIP4 facilitated HCC metastasis through the phosphorylation of PRAS40, which was regulated by mTOR. Furthermore, the expression level of Rab11-FIP4 was significantly increased in HCC tissues and high expression of Rab11-FIP4 was closely correlated with vascular invasion and poor prognosis in HCC patients. A markedly positive correlation between the expression of Rab11-FIP4 and HIF-1\α was observed in HCC tissues and combination of Rab11-FIP4 and HIF-1\α was a more valuable predictor of poor prognosis for HCC patients. In conclusion, Rab11-FIP4 is a target gene of HIF-1\α and has a pro-metastatic role in HCC, suggesting that Rab11-FIP4 may be a promising candidate target for HCC treatment.Oncogene advance online publication, 9 March 2015; doi:10.1038\\/onc.2015.49."},"journalTitle":{"0":"Oncogene"},"pdfURL":"","arrayOfPersons":[{"firstname":"dean","lastname":"wood","avatar_path":null,"username":"deanwood"},{"firstname":"dean","lastname":"wood","avatar_path":null,"username":"deanwood"},{"firstname":"Anne","lastname":"Smith","avatar_path":null,"username":"annesmith"}]}]'; function show() { var json_obj = JSON.parse(json); result = document.getElementById("result"); lastPerson = json_obj[0].arrayOfPersons[json_obj[0].arrayOfPersons.length - 1]; result.innerHTML = lastPerson.firstname + " " +lastPerson.lastname; } 
 Last person of arrayOfPersons: <div id="result"></div> <button onclick="show();">Show</button> 

if the type of return value of xmlhttp.responseText is json you doesn't need to parse it. 如果xmlhttp.responseText的返回值类型为json,则无需解析它。 You can use like this: 您可以这样使用:

var json_obj = xmlhttp.responseText;
json_obj[i].arrayOfPersons[i].firstname

如何访问 Promise 的属性<object>在 JavaScript(使用 TypeORM)中进行 DB 查询<div id="text_translate"><p>我正在使用 TypeORM 连接到 Postgres 数据库,我对 JavaScript 和 TypeORM 等真的很陌生。所以我想做的事情应该不难,是在返回 object 属性后访问它来自 Postgres 的值。</p><p> 因此,当我在 TypeORM 中使用 findOne() 方法时,它返回 Object 的 Promise 我返回,即在以下示例中,我想从数据库返回单个用户,以便我可以读取其属性并检查其密码是否匹配我搜索的用户名支持这一点。</p><pre> @Get(':username/:pwd') authUser(@Param('username') username: string, @Param('pwd') pwd: string): Promise&lt;User&gt; { const userToAuth = this.userAuthService.findOneUser(username); return userToAuth; }</pre><p> 我的问题是:有没有办法将 Promise 转换为用户 object 以便我可以访问其属性(在这种情况下为密码),以便我可以进一步检查密码是否有效? 还是有办法解决这个问题? 直接访问承诺响应也是有效的,因为当我使用 postman 时,该方法返回一个 object,如下所示:</p><pre> { name: xxxx, email: xxxx, password: xxxx, ... }</pre><p> 因此,如果我可以直接从方法中读取这些属性,那也很酷。</p><p> 我希望一切都清楚,如果您需要有关该问题的更多信息,我将很乐意提供帮助::)</p></div></object> - How to access properties of a Promise<Object> in JavaScript (using TypeORM) for DB Querying

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中,如何访问对象数组内的对象属性? - In JavaScript, how to access object properties inside an array of object? 如何使用另一个对象的属性值访问JavaScript对象的属性 - How to access properties of a JavaScript object using the property values of another object 如何在javascript中访问对象属性? - How to access object properties in javascript? 如何访问JavaScript对象的属性? - How to access the properties of a JavaScript object? Javascript对象数组访问属性未定义? - Javascript Object Array access properties undefined? 如何使用带有变量的javascript访问对象数组 - How to access object array using javascript with variable Javascript-访问对象属性 - Javascript - Access to object properties 使用Javascript访问对象属性 - Access object properties in Javascript 如何访问 Promise 的属性<object>在 JavaScript(使用 TypeORM)中进行 DB 查询<div id="text_translate"><p>我正在使用 TypeORM 连接到 Postgres 数据库,我对 JavaScript 和 TypeORM 等真的很陌生。所以我想做的事情应该不难,是在返回 object 属性后访问它来自 Postgres 的值。</p><p> 因此,当我在 TypeORM 中使用 findOne() 方法时,它返回 Object 的 Promise 我返回,即在以下示例中,我想从数据库返回单个用户,以便我可以读取其属性并检查其密码是否匹配我搜索的用户名支持这一点。</p><pre> @Get(':username/:pwd') authUser(@Param('username') username: string, @Param('pwd') pwd: string): Promise&lt;User&gt; { const userToAuth = this.userAuthService.findOneUser(username); return userToAuth; }</pre><p> 我的问题是:有没有办法将 Promise 转换为用户 object 以便我可以访问其属性(在这种情况下为密码),以便我可以进一步检查密码是否有效? 还是有办法解决这个问题? 直接访问承诺响应也是有效的,因为当我使用 postman 时,该方法返回一个 object,如下所示:</p><pre> { name: xxxx, email: xxxx, password: xxxx, ... }</pre><p> 因此,如果我可以直接从方法中读取这些属性,那也很酷。</p><p> 我希望一切都清楚,如果您需要有关该问题的更多信息,我将很乐意提供帮助::)</p></div></object> - How to access properties of a Promise<Object> in JavaScript (using TypeORM) for DB Querying 如何使用字符串/键数组访问 object 属性? - How can I access object properties using an array of strings / keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM