如何计算重复项目的计数以及那里的状态
How to calculate count for duplicated Item along with there status
_dataDetails = {
name : test1 ,
status : found,
other : []
}
{
name : test1 ,
status : found,
other : []
}
{
name : test1 ,
status : Not found,
other : []
}
{
name : test2 ,
status : found,
other : []
}etc ........
我想计算每个名字的已找到和未找到的计数。
For example:
test1
found:2
notfound :1
test2
found:1
notfound :0
可以编码
var _data = []
var index = -1;
for ( var _i = 0; _i < _data.length; _i++ )
{
if ( _data[_i][0] == _dataDetails[i].name ) { index = _i; }
}
if ( index >= 0 )
{
if ( _dataDetails[i].status == 'found' )
{
_data[index][1][0]++;
}
else
{
_data[index][1][1]++;
}
}
else
{
if ( _dataDetails[i].result == 'Detected' )
{
_data.push([_dataDetails[i].name, [1, 0]]);
}
else
{
_data.push([_dataDetails[i].name, [0, 1]]);
}
}
这里我得到双倍计数(而不是10,它将给出20)。请建议。
你可以这样编码
var _dataDetails = [{
name : test1 ,
status : found,
other : []
}
{
name : test1 ,
status : found,
other : []
}
{
name : test1 ,
status : Not found,
other : []
}
{
name : test2 ,
status : found,
other : []
}];
var counter = {};
for(index in _dataDetails) {
var dt = _dataDetails[index];
if(counter[dt.name]) {
counter[dt.name] += 1;
} else {
counter[dt.name] = 1;
}
}
console.log(counter);
你可以这样做。
_dataDetails = [{
name : "test1" ,
status : "found",
other : []
},
{
name : "test1" ,
status : "found",
other : []
},
{
name : "test1" ,
status : "Not found" ,
other : []
},
{
name :"test2" ,
status : "found",
other : []
}]
var results={};
_dataDetails.forEach ( function (o,i) {
if(!results[o.name]) {
results[o.name] = {} ;
results[o.name].found = 0;
results[o.name].notfound = 0;
}
if (o.status === "found" ) {
results[o.name].found++}
else if (o.status === "Not found" ) {results[o.name].notfound++}
});
Object.getOwnPropertyNames(results).forEach(function(prop) {
console.log( prop + " found : " + results[prop].found + " , not found: "+ results[prop].notfound);
});
请看这里:http://jsbin.com/kavesu/edit?js,console
相关文章:
- 事件和状态
- 获取选择框的状态
- 相位器状态未捕获参考错误
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何使用密码检测网络中的状态连接
- Ember.js-接口状态应该存储在哪里
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- 在Angular 2中布线期间保持零部件处于活动状态
- 在mvc应用程序中,在回发时保留最初隐藏的文本框的隐藏或可见状态
- XMLHttpRequest未返回值-状态202
- 使用javascript反复检查用户在facebook上的登录状态
- 如何使bxslider仅在移动视图中处于活动状态
- 获取ASP.NET Ajax Timer状态
- React redux初始化功能,无论状态变化如何
- 无法在现有状态转换期间更新-未使用任何非法的setState()
- 悬停下拉菜单即使在鼠标移出后也保持活动状态
- 从组件状态的数组中删除元素
- iOS 中的按钮触摸状态
- 使用AngularJS中的UI路由器将状态重定向到默认子状态
- 如何计算重复项目的计数以及那里的状态