Javascript Jquery,打印json值的出现次数

Javascript Jquery,打印json值的出现次数,javascript,jquery,json,Javascript,Jquery,Json,我正在打印 true: 5, false: 3, www.google.com: 4 在控制台/警报中,检查特定值在数据中出现的时间 下面是我的数据 data = [ { "active": true, "attributes": { "exclusiveBrand": true, "pickup": false }, "availability": {

我正在打印

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"
                }
            }
        }
    }
]
提前谢谢你的帮助

问候,,
Satz未经测试,尚未运行,应该非常接近,但这是我的第一次迭代:

function findNestedValues(arrayOfValues, nestedObject) {
  output = {};
  arrayOfValues.forEach(value => output[value.toString()] = 0);

  for (key in nestedObject) {
    currentVal = nestedObject[key];
    currentValStr = String(currentVal);
    if (typeof currentVal === 'object') {
      nested = findNestedValues(arrayOfValues, currentVal);
      for (subkey in nested) {
        output[subkey] += nested[subkey];
      }
    }
    else if (output.hasOwnProperty(currentValStr)) {
      output[currentValStr]++;
    }
  }

  return output;

}

(已知的错误是,
typeof
将返回对象、数组、null和函数的'object',因此我将在一分钟内查找更好的方法来实现这一点…但是如果您的输入可以进行质量控制,那么现在应该可以用了…而且,NaN在这里是相等的。)

未测试,还不起作用,应该非常接近,但这是我的第一次迭代:

function findNestedValues(arrayOfValues, nestedObject) {
  output = {};
  arrayOfValues.forEach(value => output[value.toString()] = 0);

  for (key in nestedObject) {
    currentVal = nestedObject[key];
    currentValStr = String(currentVal);
    if (typeof currentVal === 'object') {
      nested = findNestedValues(arrayOfValues, currentVal);
      for (subkey in nested) {
        output[subkey] += nested[subkey];
      }
    }
    else if (output.hasOwnProperty(currentValStr)) {
      output[currentValStr]++;
    }
  }

  return output;

}

(已知的错误是,
typeof
将为对象、数组、null和函数返回'object',因此我将在一分钟内找到一种更好的方法来实现这一点……但如果您的输入可以进行质量控制,那么现在就应该可以了……而且,NaN在这里是相等的。)使用
String.replace
方法解决此问题的方法出人意料

var data = [//initial data from OP
];
var anyVar = JSON.stringify(data); // we want string
var stat = {}; //here we collect result
var re=/(false|true|www\.google\.com)/g; //what we are looking for
anyVar.replace(re, function(m, pat){
  //this is the trick
  stat[pat] = (stat[pat] || 0) + 1; //count each captured pattern
  return m; //no need to change the string
});
console.log(stat); // Object { true=5,  false=3,  www.google.com=4}
更新 更详细一点的正则表达式

var re=/(?::\"?)(\bfalse\b|\btrue\b|www\.google\.com)(?:\/?\"?[,}])/g; 
//value follows after : and possible " (?: for non-capturing.
//the value may be closed by quot (") and followed by comma (,) or curly bracket (})
//this way "true love" and "false intends" are excluded.

使用
String.replace
方法解决此问题的方法出人意料

var data = [//initial data from OP
];
var anyVar = JSON.stringify(data); // we want string
var stat = {}; //here we collect result
var re=/(false|true|www\.google\.com)/g; //what we are looking for
anyVar.replace(re, function(m, pat){
  //this is the trick
  stat[pat] = (stat[pat] || 0) + 1; //count each captured pattern
  return m; //no need to change the string
});
console.log(stat); // Object { true=5,  false=3,  www.google.com=4}
更新 更详细一点的正则表达式

var re=/(?::\"?)(\bfalse\b|\btrue\b|www\.google\.com)(?:\/?\"?[,}])/g; 
//value follows after : and possible " (?: for non-capturing.
//the value may be closed by quot (") and followed by comma (,) or curly bracket (})
//this way "true love" and "false intends" are excluded.

disso在工作示例中的评论:

var obj=data[0];
var countValInObj=函数(obj,val1,val2){
变量计数=[0,0];
var checkObj=函数(x){
for(x中的var键){
如果(x[键]的类型=='对象'){
checkObj(x[键]);
}else if(x[键]==值1){
计数[0]++;
}如果(x[键]==val2),则为else{
计数[1]++;
}
}
}
检查obj(obj);
返回计数;
}

n=countValInObj(obj,真,假)工作示例中disso的注释:

var obj=data[0];
var countValInObj=函数(obj,val1,val2){
变量计数=[0,0];
var checkObj=函数(x){
for(x中的var键){
如果(x[键]的类型=='对象'){
checkObj(x[键]);
}else if(x[键]==值1){
计数[0]++;
}如果(x[键]==val2),则为else{
计数[1]++;
}
}
}
检查obj(obj);
返回计数;
}

n=countValInObj(obj,真,假)是否有您想要计算的特定值或属性?请显示您尝试过的值或属性。在这种情况下,由于您遇到不同深度的值,对于键入对象
循环,递归
可能是一种方法。是否有特定的值或属性需要计算?请显示您尝试过的内容。在这种情况下,由于您遇到不同深度的值,因此对于键入对象
循环,递归
可能是一种方法。这是一个很好的意外解决方案。不过,它可能有一些意想不到的行为。也就是说,名为“TrueTory”的钥匙也将被计算为true。“真”和“真”之间没有区别,什么可以阻止使用
\b真\b
?这是一个很好的意外解决方案。不过,它可能有一些意想不到的行为。也就是说,名为“TrueTory”的钥匙也将被计算为true。“真”和“真”之间没有区别什么可以阻止使用
\b真\b