JavaScript - 计算字符串的出现次数

时间:2017-08-04 10:29:35

标签: javascript json

我希望计算JSON中某些字符串的出现次数 - 在本例中为sensorUUID

var newDataArray = JSON.stringify(conData);

JSON

[{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}]

我尝试了以下代码,但它返回了一个空对象。

var obj = {};

for (var i = 0, j = newDataArray.length; i < j; i++) {
  if (obj[newDataArray[i].sensorUUID]) {
    obj[newDataArray[i]]++;
  }
}

console.log(obj);

完整的JSON文件中将包含多个传感器ID,我希望返回唯一传感器ID的数量。

e.g。

22489710有10次出现

63846683有23次出现

等。

4 个答案:

答案 0 :(得分:2)

for循环中的if条件是正确的,但是第一次找到特定的sensorUUID时必须将count初始化为1。

var newDataArray = [{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}];

var obj = {};
for (var i = 0, j = newDataArray.length; i < j; i++) {
  if (obj[newDataArray[i].sensorUUID]) {
     obj[newDataArray[i].sensorUUID]++;
  }else{
     obj[newDataArray[i].sensorUUID] = 1;
  }
}
// obj gives you count for each unique sensorUUID.
console.log(obj);

//if you want total count of all sensorUUID you can sum all the values in obj.
var count = Object.values(obj).reduce((a, b) => a + b, 0);
console.log(count);

答案 1 :(得分:0)

您可以使用jsonarray.reduce轻松遍历count数组sensorUUID,并将其存储在新对象中。

&#13;
&#13;
var json = [{
  "blobJson": "x",
  "deviceMfg": 10,
  "eventCode": 0,
  "sensorClass": 3,
  "sensorUUID": "22489710",
  "timeStamp": 1500362037.111941,
  "uID": "22489710_3_10"
}, {
  "blobJson": "x",
  "deviceMfg": 10,
  "eventCode": 0,
  "sensorClass": 3,
  "sensorUUID": "22489710",
  "timeStamp": 1500362037.111941,
  "uID": "22489710_3_10"
}, {
  "blobJson": "x",
  "deviceMfg": 10,
  "eventCode": 0,
  "sensorClass": 3,
  "sensorUUID": "22489710123",
  "timeStamp": 1500362036.109874,
  "uID": "22489710_3_10"
}];

let count = json.reduce((newObj, obj) => {
  if(newObj[obj.sensorUUID]) {
    newObj[obj.sensorUUID] = newObj[obj.sensorUUID]+1;
  } else {
    newObj[obj.sensorUUID] = 1;
  }
  return newObj;
}, {});

console.log(count);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用Array#forEach设置变量计数并迭代数组,并使用Object#hasOwnProperty检查对象是否具有 sensorUUID 属性,如果是,则递增计数

&#13;
&#13;
var data = [{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}];
var count = 0;
data.forEach((x)=>{
  if(x.hasOwnProperty('sensorUUID'))
    count++;
});
console.log(count);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

https://jsfiddle.net/7jjoches/1/

使用jquery方法$ .parseJSON你必须将JSON字符串转换为JSON对象,然后才能使用它。

var conData = '[{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}]';

var newDataArray = $.parseJSON(conData);

console.dir(newDataArray);

var obj = {};

for (var i = 0; i< newDataArray.length; i++) {
    obj[newDataArray[i].sensorUUID] = obj[newDataArray[i].sensorUUID] ? obj[newDataArray[i].sensorUUID]+1 : 1;
}

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>