簡體   English   中英

JSON arrary訪問未定義

[英]JSON arrary accessing undefined

我在訪問簡單JSON數組的值時遇到困難...在這種情況下,如何提取“ testresult”? 提前致謝

var submittedSearchJSON = [];
submittedSearchJSON.push({"test": "testresult"}); 
console.log(submittedSearchJSON)
console.log(submittedSearchJSON.test)
console.log(submittedSearchJSON["test"])

以上結果導致:[{test:“ testresult”}]未定義未定義

您忘記了使用索引先訪問Array對象

var submittedSearchJSON = [];
submittedSearchJSON.push( {"test": "testresult"} );

console.log( submittedSearchJSON ); // [object Array]
console.log( submittedSearchJSON[0].test ); // "testresult"
console.log( submittedSearchJSON[0]["test"] ); // "testresult"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM