在Javascript中计算数据集中每个状态的值
Posted
技术标签:
【中文标题】在Javascript中计算数据集中每个状态的值【英文标题】:Counting the value for each status in a data set in Javascript 【发布时间】:2017-03-26 03:13:27 【问题描述】:来自变量:
var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
我如何得到这个结果:
已满 = 5 空 = 2
我需要它来根据纬度和经度确定我大学的废物管理
【问题讨论】:
【参考方案1】:考虑到locations
数组中的所有子数组都按原样布局,您可以简单地使用for loop
来检查每个子数组的第一个元素的值是"FULL"
还是@ 987654324@。
例子:
var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
var full = 0;
var empty = 0;
for(var i = 0; i < locations.length; i++)
if(locations[i][0] == 'FULL') full++;
if(locations[i][0] == 'EMPTY') empty++;
console.log("full " + full + " empty " + empty );
如果"EMPTY"
或"FULL"
可能位于子数组中的任何位置,即不能保证它们始终位于索引0
处,那么您可以使用以下解决方案:
var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
var full = 0;
var empty = 0;
for(var i = 0; i < locations.length; i++)
for(var j = 0; j < locations[i].length; j++)
if(locations[i][j] == 'FULL') full++;
if(locations[i][j] == 'EMPTY') empty++;
console.log("full " + full + " empty " + empty );
【讨论】:
【参考方案2】:有很多方法,这是其中之一:
var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
res = ;
locations.forEach(item=>res.hasOwnProperty(item[0]) ? res[item[0]]++ : res[item[0]] = 1);
console.log(res);
JSFiddle:https://jsfiddle.net/k94qfa9r/2/
如果你需要它作为一个函数:
var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
function countItems(locs)
res = ;
locs.forEach(item=>res.hasOwnProperty(item[0]) ? res[item[0]]++ : res[item[0]] = 1);
return res;
console.log(countItems(locations));
https://jsfiddle.net/k94qfa9r/4/
如果你需要它作为数组:
var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
function countItems(locs)
res = ;
resArr = [];
locs.forEach(item=>res.hasOwnProperty(item[0]) ? res[item[0]]++ : res[item[0]] = 1);
for(var p in res)
resArr.push([p, res[p]])
return resArr;
console.log(countItems(locations));
JSFiddle:https://jsfiddle.net/k94qfa9r/6/
【讨论】:
以上是关于在Javascript中计算数据集中每个状态的值的主要内容,如果未能解决你的问题,请参考以下文章