[英]How can I count the occurrence of a value of an attribute in an array of anonymous objects
[英]How can I reference an anonymous json array?
我有一個很大的JSON對象,該對象是從QuickBooks Online API返回的。 它是有效的JSON,並顯示在控制台上(記錄后)。
檢查控制台后,我看到以下內容:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
可以擴展為:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
9: Object
10: Object
11: Object
12: Object
13: Object
14: Object
15: Object
16: Object
17: Object
18: Object
19: Object
20: Object
21: Object
22: Object
23: Object
24: Object
length: 25
__proto__: Array[0]
0: Object
擴展為:
0: Object
*_data: Object
$$hashKey: "object:82"
__proto__: Object
*_data: Object
擴展為:
Active: Array[1]
Balance: Array[1]
BalanceWithJobs: Array[1]
BillAddr: Array[1]
BillWithParent: Array[1]
DisplayName: Array[1]
FamilyName: Array[1]
Fax: Array[1]
FullyQualifiedName: Array[1]
GivenName: Array[1]
Id: Array[1]
Job: Array[1]
MetaData: Array[1]
Mobile: Array[1]
PreferredDeliveryMethod: Array[1]
PrimaryEmailAddr: Array[1]
PrimaryPhone: Array[1]
PrintOnCheckName: Array[1]
SalesTermRef: Array[1]
SyncToken: Array[1]
Taxable: Array[1]
我正在嘗試訪問一個名為DisplayName
的屬性。
用於console.log json數據的Angular代碼:
$http.get('/customer').success(function(customers) {
$scope.customers = customers;
console.log($scope.customers[0]["*_data"].DisplayName[0]);
});
如何才能做到這一點? 可以在控制台中鍵入某些內容以返回其值嗎?
更新:
我可以使用以下方法訪問該對象:
console.log($scope.customers[0]);
但是, *_data
是一個對象。 我在想類似的東西:
console.log($scope.customers[0].*_data);
可以,但是,我收到錯誤消息:
Uncaught SyntaxError: Unexpected token *
。
您是否將其記錄到控制台?
假設您是,並且您有類似以下內容:
console.log(data);
您可以像這樣訪問數據:
data[0]["*_data"].DisplayName[0]
那是:
data[0]
取消引用第一個數組項 *_data
屬性。 由於其名稱不是有效的屬性名稱,因此必須使用["string"]
形式。 DisplayName
值是一個數組。 用[0]
取第一個值。 好的,哇,這實際上是很有趣的一個!
似乎響應數據包含一個奇怪的字符,這使得訪問響應變得很棘手。 特別:
> Object.keys($scope.customers[0])[0]
"*_data"
> Object.keys($scope.customers[0])[0] == "*_data"
false
?? 他們為什么不相等? 這些字符之一必須是Unicode字符,它看起來相同,但值不同!
無論如何,解決這個問題很容易:
var mysteriousKey = Object.keys($scope.customers[0])[0];
var data = $scope.customers[0][mysteriousKey];
這給了我:
對象{Id:Array [1],SyncToken:Array [1],MetaData:Array [1],GivenName:Array [1],FamilyName:Array [1]…}
我認為這是你的追求。
您的數組應分配給變量。 在所需的數組中引用該項目的索引,並訪問它的DisplayName
屬性(也是一個數組)。
var objArray; // this has your array of objects
objArray[0]["*_data"].DisplayName[0]; // should be the value you are looking for.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.