简体   繁体   中英

count occurrence word in array javascript

i have data result:

{
  "statusCode": "T",
  "statusMessage": "Success",
  "resultFeasibility": {
    "resultStatus": 7005,
    "resultInfo": "Fiber
     access is available"
  },
  "devices": {
    "deviceID": "2573631",
    "hostName": "ODP-SKB-FCP\/003 FCP\/D01\/003.01",
    "networkLocation": "ODP-SKB-FCP\/003",
    "technology": "GPON",
    "stoCode": "SKB",
    "maxSpeed": null,
    "double": 0,
    "icon": "resources\/images\/icon-gphone-blue.png",
    "address": {
      "latitude": "-6.915",
      "longitude": "106.9185",
      "zipCode": null,
      "district": null,
      "city": null,
      "streetName": null,
      "lineOne": null
    },
    "markerId": "7-2573631",
    "type": "ALPRO"
  }
}

i want to show how much 'deviceID' occurrence,this is my code: code :

if (result.statusCode == 'T') {
  this.odp = result.devices;
  console.log(this.odp);
  if (this.odp.length > 0) {
    var alproType = this.odp.technology;
    if (alproType == 'DSLAM')
      this.feasibilityResult.push('feasibility DSLAM: ' + this.odp.length + ' DSLA');
    else if (alproType == 'MSAN')
      this.feasibilityResult.push('feasibility MSAN: ' + this.odp.length + ' MSAN');
    else
      this.feasibilityResult.push('feasibility ODP: ' + this.odp.length + ' ODP');
  }

but the output is null. anybody can help me? Thanks

What happens is that devices isn't an array that's why don't have length, you must to change the json to:

{"statusCode":"T","statusMessage":"Success","resultFeasibility":{"resultStatus":7005,"resultInfo":"Fiber
 access is available"},"devices":[{"deviceID":"2573631","hostName":"ODP-SKB-FCP\/003 FCP\/D01\/003.01"
,"networkLocation":"ODP-SKB-FCP\/003","technology":"GPON","stoCode":"SKB","maxSpeed":null,"double":0
,"icon":"resources\/images\/icon-gphone-blue.png","address":{"latitude":"-6.915","longitude":"106.9185"
,"zipCode":null,"district":null,"city":null,"streetName":null,"lineOne":null},"markerId":"7-2573631"
,"type":"ALPRO"}]}

I mean put devices inside brakets [] and then you can call the property length. Otherwise you must change the condition, for example

if(this.odp != undefined){...}

Hope this help

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