简体   繁体   中英

JavaScript - count occurrence of strings

I'm looking to count the occurances of certain strings within JSON - in this instance 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"}]

I've tried the following code but it is returning an empty object.

var obj = {};

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

console.log(obj);

The full JSON file will have multiple sensor ID's within it, I am looking to return the number of unique sensor ID.

eg

22489710 has 10 occurrences

63846683 has 23 occurrences

etc.

the if condition in for loop is correct but you have to initialize count as 1 for the first time you find a particular sensorUUID.

 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); 

You can simply iterate through the json array using array.reduce and count the occurances of sensorUUID and store it inside the new object.

 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); 

you can set a variable count and iterate over the array using Array#forEach and check whether the object has the property sensorUUID using Object#hasOwnProperty if yes, increment the count

 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); 

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

Using jquery method $.parseJSON you have to convert the JSON string to a JSON object and only then you can work with it.

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM