计算数组中单词的出现次数 javascript
count occurrence word in array javascript
我有数据结果:
{
"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"
}
}
我想显示有多少 'deviceID' 出现,这是我的代码:
代码:
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');
}
但输出为空。有人可以帮助我吗?谢谢
发生的情况是设备不是数组,这就是为什么没有长度,您必须将 json 更改为:
{"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"}]}
我的意思是将设备放在括号内 [] 然后你可以调用 属性 长度。
否则你必须改变条件,例如
if(this.odp != undefined){...}
希望对您有所帮助
我有数据结果:
{
"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"
}
}
我想显示有多少 'deviceID' 出现,这是我的代码: 代码:
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');
}
但输出为空。有人可以帮助我吗?谢谢
发生的情况是设备不是数组,这就是为什么没有长度,您必须将 json 更改为:
{"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"}]}
我的意思是将设备放在括号内 [] 然后你可以调用 属性 长度。 否则你必须改变条件,例如
if(this.odp != undefined){...}
希望对您有所帮助