我有一个数组
data = [ '2016-12-12 pass [pass]',
'2016-12-12 pass [pass]',
'2016-12-12 fail [fail]',
'2016-12-13 fail [fail]',
'2016-12-14 fail [fail]',
'2016-12-14 pass [pass]',
'2016-12-14 pass [pass]',
'2016-12-15 pass [pass]',
'2016-12-16 fail [fail]' ]
我尝试了很多方法来完成使用各种功能,但无法找到如何实现这种格式:
2016-12-12 pass:2 fail: 1
2016-12-13 pass:0 fail: 1
2016-12-14 pass:2 fail: 1
2016-12-15 pass:1 fail: 0
2016-12-16 pass:0 fail: 1
答案 0 :(得分:2)
以这种方式:
const data = [
'2016-12-12 pass [pass]',
'2016-12-12 pass [pass]',
'2016-12-12 fail [fail]',
'2016-12-13 fail [fail]',
'2016-12-14 fail [fail]',
'2016-12-14 pass [pass]',
'2016-12-14 pass [pass]',
'2016-12-15 pass [pass]',
'2016-12-16 fail [fail]',
];
const result = {};
data.forEach(item => {
const [date, passFail] = item.split(' ');
if (!(date in result)) {
result[date] = { pass: 0, fail: 0 };
}
result[date][passFail] += 1;
});
console.log(result);

正如您在输出中看到的,这将返回一个对象,其属性名称为日期,其值为具有属性pass
和fail
的对象。
请注意,您可以使用reduce
而不是forEach
来保存一行,但如果您要对该对象进行变更,那么reduce
就毫无意义,所以我&#39 ;为了简单起见,使用了forEach
。
另请注意,虽然在实践中(即在当前的JS引擎中)对象保留了它们的属性。插入顺序,严格来说JavaScript对象属性是无序的。如果你想对订单严格要求,那么你需要使用一个数组,在这种情况下,最简单的解决方案就是如下所述的事后转换:
const result = {
"2016-12-12": { pass: 2, fail: 1 },
"2016-12-13": { pass: 0, fail: 1 },
"2016-12-14": { pass: 2, fail: 1 },
"2016-12-15": { pass: 1, fail: 0 },
"2016-12-16": { pass: 0, fail: 1 },
};
const resultArray = Object.keys(result).sort()
.map(key => [ key, result[key] ]);
console.log(resultArray);

答案 1 :(得分:1)
您可以使用reduce()
并返回对象数组。
var data = [ '2016-12-12 pass [pass]',
'2016-12-12 pass [pass]',
'2016-12-12 fail [fail]',
'2016-12-13 fail [fail]',
'2016-12-14 fail [fail]',
'2016-12-14 pass [pass]',
'2016-12-14 pass [pass]',
'2016-12-15 pass [pass]',
'2016-12-16 fail [fail]' ]
var result = Object.values(data.reduce(function(r, e) {
var arr = e.split(' ')
if(!r[arr[0]]) r[arr[0]] = {date: arr[0], pass: 0, fail: 0}
r[arr[0]][arr[1]]++
return r;
}, {}))
console.log(JSON.stringify(result))
如果你不能使用Object.values()
,你最后可以在对象上使用map。
var data = [ '2016-12-12 pass [pass]',
'2016-12-12 pass [pass]',
'2016-12-12 fail [fail]',
'2016-12-13 fail [fail]',
'2016-12-14 fail [fail]',
'2016-12-14 pass [pass]',
'2016-12-14 pass [pass]',
'2016-12-15 pass [pass]',
'2016-12-16 fail [fail]' ]
var result = data.reduce(function(r, e) {
var arr = e.split(' ')
if(!r[arr[0]]) r[arr[0]] = {date: arr[0], pass: 0, fail: 0}
r[arr[0]][arr[1]]++
return r;
}, {})
result = Object.keys(result).map(e => result[e])
console.log(JSON.stringify(result))
答案 2 :(得分:1)
您可以使用reduce
将日期哈希到这样的对象中:(注意输出是对象的对象)
function group(arr) {
return arr.reduce(function(hash, e) { // for each element e in the array arr
var parts = e.split(' '); // split the element e by space
if(hash[parts[0]]) { // if the date (part 0) is already hashed
hash[parts[0]][parts[1]]++; // then increment the equivalent pass or fail (part 1)
}
else { // if not
hash[parts[0]] = {pass: 0, fail: 0}; // create a new object for the date
hash[parts[0]][parts[1]]++; // increment its pass or fail value
}
return hash;
}, {});
}
var array = ['2016-12-12 pass [pass]',
'2016-12-12 pass [pass]',
'2016-12-12 fail [fail]',
'2016-12-13 fail [fail]',
'2016-12-14 fail [fail]',
'2016-12-14 pass [pass]',
'2016-12-14 pass [pass]',
'2016-12-15 pass [pass]',
'2016-12-16 fail [fail]'
];
console.log(group(array));