按日期对对象进行分组的最佳方法是什么,并将它们全部添加到创建单个JavaScript对象?我更喜欢代码可读性和较少的代码行而不是性能,因为应用程序不是数据密集型的。此外,在我完成实习后,会有很多人会做出改变。
以下是一些示例数据
[
{"Timestamp":"Thu, 01 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 01 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 01 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 01 Jan 2015 03:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 02 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 02 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 02 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 02 Jan 2015 03:57:00 GMT","cp":0,"pc":4}
]
我想从上述数据中创建它。
[ {"Date":"2015-01-01", "cp":4, "pc":12}, {"Date":"2015-01-02", "cp":4, "pc"12} ]
如果您注意到,生成的JavaScript对象是该特定日期所有列的总和,并按日期替换时间戳。
我们已在应用程序中使用moment.js,因此我可以使用它。如果您认为时刻能够以更整洁的方式解决问题,请告诉我。
答案 0 :(得分:2)
$.each()
函数可用于迭代任何集合。) UTC
方法将ISO
格式的日期字符串转换为toISOString()
格式。indexOf()
方法轻松映射重复的日期位置。以下是代码:
var arr = [
{"Timestamp":"Thu, 01 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 01 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 01 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 01 Jan 2015 03:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 02 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 02 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 02 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 02 Jan 2015 03:57:00 GMT","cp":0,"pc":4}
];
var resultArr = [];
var dateArr = [];
$.each(arr, function () {
// Easiest way to get your required date format
var date = new Date(this.Timestamp).toISOString().replace(/T/, ' ').split(' ')[0];
var index = dateArr.indexOf(date);
if (index == -1) {
dateArr.push(date);
var obj = {Date: date, cp: this.cp, pc: this.pc};
resultArr.push(obj);
}
else {
resultArr[index].cp += this.cp;
resultArr[index].pc += this.pc;
}
});
console.log(resultArr); // [ {"Date":"2015-01-01", "cp":4, "pc":12}, {"Date":"2015-01-02", "cp":4, "pc"12} ]
希望这对你有所帮助。
答案 1 :(得分:1)
我相信一个简单的实用程序库,例如Underscore.js或Lodash可以完成这项工作,但是,这看起来像一个简单的旧vanilla Javascript可以自己承担的任务。
查看Array.prototype.map函数,或者更好的是Array.prototype.sort,它接受一个将逻辑应用于给定对象的.Timestamp
属性的排序函数。
编辑:类似
yourArray.sort(function(objA, objB){
return objA.Timestamp > objB.TimeStamp ? -1 : 1;
});
可能会奏效。
编辑:我现在看到你正在尝试合并日期。在这种情况下,我会说Array.prototype.map
与Array.prototype.reduce
链接在一起可能会导致失败。你也可以(乱七八糟地)使用Array.prototype.filter
将给定日期的所有数组推送到一个数组中,依此类推每个日期。
编辑:
那么减少呢?
var thingYouWant = yourArray.reduce(function(summed, current){
date = changeIntoOnlyDayDate(current.Timestamp);
var found = false;
summed.forEach(function(obj){
if(obj.Timestamp === date){ obj.cp++; found = true;};
}
if(!found){summed.push(turnThisDateIntoDesiredObject(date))}
}
return summed;
}, []);
答案 2 :(得分:1)
试试这个。
var timeArray=[
{"Timestamp":"Thu, 01 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 01 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 01 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 01 Jan 2015 03:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 02 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 02 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
{"Timestamp":"Thu, 02 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
{"Timestamp":"Thu, 02 Jan 2015 03:57:00 GMT","cp":0,"pc":4}
]
var object = {}, resultArray = [];
for (var i = 0; i < timeArray.length; i++) {
var date = timeArray[i].Timestamp;
var localDateString = new Date(date).getFullYear() + '-' + (new Date(date).getMonth() + 1) + '-' + new Date(date).getDate();
if (object[localDateString]) {
object[localDateString].cp += timeArray[i].cp;
object[localDateString].pc += timeArray[i].pc;
} else {
timeArray[i].Date = localDateString;
delete timeArray[i].Timestamp;
object[localDateString] = timeArray[i];
}
}
for (var prop in object)
resultArray.push(object[prop]);
console.log(resultArray);