gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:26:21 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com