Javascript 确定JSON数组中出现的字符串

Javascript 确定JSON数组中出现的字符串,javascript,arrays,json,string,Javascript,Arrays,Json,String,我有一个字符串化数组: JSON.stringify(arr) = [{"x":9.308,"y":6.576,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25},{"x":9.42,"y":7.488,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shap

我有一个字符串化数组:

JSON.stringify(arr) = [{"x":9.308,"y":6.576,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25},{"x":9.42,"y":7.488,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25}]
我需要知道单词yellow出现了多少次,这样我就可以做如下事情:

numYellow = 0;
for(var i=0;i<arr.length;i++){
  if(arr[i] === "yellow")
  numYellow++;
}

doSomething = function() {
  If (numYellow < 100) {
    //do something
  }
  If(numYellow > 100) {
    //do something else
  } else { do yet another thing} 
  }
numYellow=0;
对于(变量i=0;i 100){
//做点别的
}否则{再做一件事}
}

数组的每个元素都是一个对象。将
arr[i]
更改为
arr[i]。颜色
。这确实假设
.color
属性是唯一存在
黄色的点。

这应该可以做到:

var array = [{"x":9.308,"y":6.576,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25},{"x":9.42,"y":7.488,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25}]

var numYellow = 0;

for(var i=0; i<array.length; i++) {
    if (array[i].color === "yellow") {
        numYellow++;
    }
}
var数组=[{“x”:9.308,“y”:6.576,“颜色”:“黄色”,“恢复”:0.2,“类型”:“静态”,“半径”:1,“形状”:“正方形”,“宽度”:0.25,“高度”:0.25},{“x”:9.42,“y”:7.488,“颜色”:“黄色”,“恢复”:0.2,“类型”:“静态”,“半径”:1,“形状”:“正方形”,“宽度”:0.25,“高度”:0.25}]
var numYellow=0;

对于(var i=0;i
.stringify()
与此有什么关系?如果它真的是stringify,就不能像您现在这样进行迭代。您可以不用stringify数组来进行迭代。请参阅@Ashwins answer。我的荣幸!很乐意为您提供帮助