簡體   English   中英

如何使用javascript訪問位於對象數組中的對象的屬性?

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

以下是來自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"
    }]
}]

我將字符串轉換為javascript對象:

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

我正在嘗試從此元素(位於元素的末尾)訪問對象數組的屬性,但未成功。 我嘗試了以下操作,但未成功:

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

請您能指教嗎?

首先:修復您的JSON,結尾缺少一個]。 除此之外,您可能還想遍歷所有可能的名字。 嘗試這個:

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);
    }
}

獲取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> 

如果xmlhttp.responseText的返回值類型為json,則無需解析它。 您可以這樣使用:

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中,如何訪問對象數組內的對象屬性? 如何使用另一個對象的屬性值訪問JavaScript對象的屬性 如何在javascript中訪問對象屬性? 如何訪問JavaScript對象的屬性? Javascript對象數組訪問屬性未定義? 如何使用帶有變量的javascript訪問對象數組 Javascript-訪問對象屬性 使用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> 如何使用字符串/鍵數組訪問 object 屬性?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM