javascript - 根据类别和子类别总结 JavaScript 数组

标签 javascript arrays string

我一直在尝试获取数组中元素的摘要。该数组包含不同类别的不同值,我想在用户检查数组内容之前总结数组的内容。 这是数据:

var typed = [
[1, 'MAP: Invalid service'],
[2, 'MAP: map observation'],
[3, 'ADT: Invalid person'],
[4, 'ADT:  verification failed'],
[5, 'SYSTEM: Unrecognized'],
[6, 'SYSTEM: Processing exempt']
[7, 'SYSTEM: Data access'],
[8, 'SYSTEM: Data access'],
[9, 'SYSTEM: Transform Exempt'],
[10, 'SYSTEM: Transform Exempt'],
[11, 'ADT:  verification failed'],
[12, 'ADT: Invalid person']
]

我想要这种性质的东西作为摘要

  4 ADT :
          2 Invalid person
          2 verification failed

 2 MAP: 
          1 Invalid service
          1 map observation

 6 SYSTEM: 
          1 Unrecognized
          1 Processing exempt
          2 Transform exempt
          2 Data access

---------------------
Total: 12

这是我尝试过的:

function summary(typed, category){
    var arrA = [];
    var arrB = [];
    for (var i=0; i<typed.length; i++){
        var tryb = typed[i][4].split(' ');
        for (var j=0; j< category.length; j++){
            if (tryb[0] == category[j]){
            arrA[i][j] = typed[i];
            }
        }
    }
    return arrA;
}
var newcategory = ['ADT:','MAP:'];
summary(typed, newcategory)

最佳答案

有很多方法可以滚动浏览数据、拆分、合并和计数。这是一个。

JSFiddle here.

鉴于您提供的列表(在您的示例中索引 6 缺少结束逗号):

var typed = [
[1, 'MAP: Invalid service'],
[2, 'MAP: map observation'],
[3, 'ADT: Invalid person'],
[4, 'ADT:  verification failed'],
[5, 'SYSTEM: Unrecognized'],
[6, 'SYSTEM: Processing exempt'], // added comma
[7, 'SYSTEM: Data access'],
[8, 'SYSTEM: Data access'],
[9, 'SYSTEM: Transform Exempt'],
[10, 'SYSTEM: Transform Exempt'],
[11, 'ADT:  verification failed'],
[12, 'ADT: Invalid person']
]

function summary(typed, category){
    var arrA = {};
    var pass = 0; // pass is used to keep track of type matches below
    for (var i=0; i<typed.length; i++){
        // split on colon and any number spaces gives 2 elements in return
        var tryb = typed[i][1].split(/: +/);
        // if mode does not exist
        if ( !arrA[tryb[0]] ) {
            arrA[tryb[0]] = {};
            arrA[tryb[0]].counter = 1;
            arrA[tryb[0]].types = [];
            arrA[tryb[0]].types.push([1, tryb[1]]);
        }
        // mode does exist
        else {
            arrA[tryb[0]].counter += 1;
            pass = 0;
            // check for existing type
            arrA[tryb[0]].types.forEach(function(val, idx, arr) {
                if ( tryb[1] === val[1] ) {
                    // exists so increment counter
                    arr[idx][0] += 1;
                    pass = 1;
                }
            });
            // newly found type, add to array and count
            if ( !pass ) {
                arrA[tryb[0]].types.push([1, tryb[1]]);
            }
        }
    }
    return arrA;
}
// get modes object
var modes = summary(typed);

// this mode object can be reported in many ways,
// a simple text report here
var modes_string = '';
var total = 0;
for ( mode in modes ) {
    modes_string += modes[mode].counter + ' ' + mode + "\n";
    modes[mode].types.forEach(function(val, idx, arr) {
        modes_string += ' ' + modes[mode].types[idx][0] + ' ' + modes[mode].types[idx][1] + "\n";
    });
    modes_string += "\n";
    total += modes[mode].counter;
}
modes_string += 'Total: ' + total;
console.log(modes_string);

输出:

2 MAP
 1 Invalid service
 1 map observation

4 ADT
 2 Invalid person
 2 verification failed

6 SYSTEM
 1 Unrecognized
 1 Processing exempt
 2 Data access
 2 Transform Exempt

Total: 12

关于javascript - 根据类别和子类别总结 JavaScript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242606/

相关文章:

javascript - 作为 Array.prototype.every() 方法的结果,我如何返回一个 bool 值数组?

c# - 字符串创建

javascript - 点击更新警报和计数器

javascript - 如何在服务器端验证 jquery 中动态创建的控件 - Php

arrays - 求数组中除0之外的最小值

c - 需要显示多个数组中前3个最大的数字

c++ - 中间函数

c - 与 '\0' 的字符串关系

javascript - 在 javascript 中使用柯里化(Currying)函数优于普通函数的优点

javascript - Firebase 不存储日期