我正在尝试打印
true: 5,
false: 3,
www.google.com: 4
在控制台/警报中检查特定值在数据中出现的次数。
下面是我的数据
data = [
{
"active": true,
"attributes": {
"exclusiveBrand": true,
"pickup": false
},
"availability": {
"pickup": {
"available": true,
"availability": false,
"checkStoresLink": {
"url": "www.google.com"
}
},
"ship": {
"available": false,
"logoUrl": "www.google.com",
"checkLink": {
"height": 470,
"targeting": "popup",
"url": "www.google.com",
"width": 530
},
"getItBy": {
"currentPrice": 109.99,
"availability": true,
"regionalAvailabilityEligible": true,
"imageUrl": "www.google.com"
}
}
}
}
]
感谢您提前提供的帮助。
问候, 萨兹
最佳答案
disso 在工作示例中的评论:
var obj = data[0];
var countValInObj = function(obj,val1,val2){
var count = [0,0];
var checkObj = function(x){
for (var key in x) {
if(typeof x[key] == 'object'){
checkObj(x[key]);
} else if(x[key] === val1) {
count[0]++;
} else if(x[key] === val2) {
count[1]++;
}
}
}
checkObj(obj);
return count;
}
n = countValInObj(obj,true,false);
关于javascript - Jquery,打印json值出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339103/