87,901
社区成员
发帖
与我相关
我的任务
分享
const jsonNow = [
{'value': '1.77', 'time': '2020-12-09 14:32:00', 'name': 'C'},
{'value': '1.77', 'time': '2020-12-09 14:37:00', 'name': 'B'}
];
const jsonHistory = [
{'value': '1.78', 'time': '2020-12-08 14:31:00', 'name': 'A'},
{'value': '1.76', 'time': '2020-12-08 14:35:00', 'name': 'A'},
{'value': '1.79', 'time': '2020-12-08 14:36:00', 'name': 'A'},
{'value': '1.66', 'time': '2020-12-08 14:35:00', 'name': 'B'},
{'value': '1.71', 'time': '2020-12-08 14:31:00', 'name': 'B'},
{'value': '1.69', 'time': '2020-12-08 14:36:00', 'name': 'B'}
];
/**
* @description 将jsonHistory以name进行分组,以便于快速查找
* @param {*} data jsonHistory
* @returns {*} 分组结果
*/
function groupByName(data) {
let group = {};
data.forEach(item => {
const key = item.name;
group[key] = group[key] || [];
group[key].push(item);
});
return group;
};
/**
* @description 将时间部份换算为秒
* @param {string} strDate 待转换的时间部份
* @returns {number} 换算结果
*/
function calcSeconds(strDate) {
const iDate = new Date(strDate);
return iDate.getHours() * 3600 + iDate.getMinutes() * 60 + iDate.getSeconds();
};
//history以name分组
const hisGroup = groupByName(jsonHistory);
//最终结果
let res = [];
//遍历jsonNow,查找最接近的时间
jsonNow.forEach(item => {
if(hisGroup[item.name]) {
const his = hisGroup[item.name].sort((a, b) => {
const nTime = calcSeconds(item.time);
const aTime = calcSeconds(a.time);
const bTime = calcSeconds(b.time);
return Math.abs(nTime - aTime) - Math.abs(nTime - bTime)
})[0];
res.push(his);
}
});
console.log(res);
const jsonNow = [
{'value': '1.77', 'time': '2020-12-09 14:32:00', 'name': 'C'},
{'value': '1.77', 'time': '2020-12-09 14:37:00', 'name': 'B'}
];
const jsonHistory = [
{'value': '1.78', 'time': '2020-12-08 14:31:00', 'name': 'A'},
{'value': '1.76', 'time': '2020-12-08 14:35:00', 'name': 'A'},
{'value': '1.79', 'time': '2020-12-08 14:36:00', 'name': 'A'},
{'value': '1.66', 'time': '2020-12-08 14:35:00', 'name': 'B'},
{'value': '1.71', 'time': '2020-12-08 14:31:00', 'name': 'B'},
{'value': '1.69', 'time': '2020-12-08 14:36:00', 'name': 'B'}
];
/**
* @description 将jsonHistory以name进行分组,以便于快速查找
* @param {*} data jsonHistory
* @returns {*} 分组结果
*/
function groupByName(data: {value: string, time: string, name: string}[]): {[key: string]: {value: string, time: string, name: string}[]} {
let group = {};
data.forEach(item => {
const key = item.name;
group[key] = group[key] || [];
group[key].push(item);
});
return group;
}
/**
* @description 将时间部份换算为秒
* @param {string} strDate 待转换的时间部份
* @returns {number} 换算结果
*/
function calcSeconds(strDate: string): number {
const iDate = new Date(strDate);
return iDate.getHours() * 3600 + iDate.getMinutes() * 60 + iDate.getSeconds();
}
//history以name分组
const hisGroup = groupByName(jsonHistory);
//最终结果
let res: {value: string, time: string, name: string}[] = [];
//遍历jsonNow,查找最接近的时间
jsonNow.forEach(item => {
if(hisGroup[item.name]) {
const his = hisGroup[item.name].sort((a, b) => {
const nTime = calcSeconds(item.time);
const aTime = calcSeconds(a.time);
const bTime = calcSeconds(b.time);
return Math.abs(nTime - aTime) - Math.abs(nTime - bTime)
})[0];
res.push(his);
}
});
console.log(res);
// 这样才对
var obj= {'value':'1.77','time':'2020-12-09 14:32:00','name':'A'};
jsonHistory.push(obj)
var data = jsonHistory.filter(v => v.name == obj.name).sort( (a, b) => new Date(b.time).getTime() - new Date(a.time).getTime() )
var index = data.map(v => v.time).indexOf(obj.time)
var res = obj
if(index == 0){ // 排除可能是第一个
res = data[index+1]
}else if(index == data.length - 1){ // 排除可能是最后一个
res = data[index - 1]
}else if(index != 0 && index != data.length - 1){ // 排除可能是只有自身
var num1 = Math.abs( new Date(data[index - 1].time).getTime() - new Date(obj.time).getTime() )
var num2 = Math.abs( new Date(data[index + 1].time).getTime() - new Date(obj.time).getTime() )
res = num1 > num2 ? data[index + 1] : data[index - 1]
}
console.log(res) // 结果
// 这样才对
var obj= {'value':'1.77','time':'2020-12-09 14:32:00','name':'A'};
jsonHistory.push(obj)
var data = jsonHistory.filter(v => v.name == obj.name).sort( (a, b) => new Date(b.time).getTime() - new Date(a.time).getTime() )
var index = data.map(v => v.time).indexOf(obj.time)
var num1 = Math.abs( new Date(data[index - 1].time).getTime() - new Date(obj.time).getTime() )
var num2 = Math.abs( new Date(data[index + 1].time).getTime() - new Date(obj.time).getTime() )
var res = num1 > num2 ? data[index + 1] : data[index - 1]
console.log(res) // 结果
[/quote]
我运行的时候,出错,Uncaught TypeError: Cannot read property 'time' of undefined
at test.html:27
// 这样才对
var obj= {'value':'1.77','time':'2020-12-09 14:32:00','name':'A'};
jsonHistory.push(obj)
var data = jsonHistory.filter(v => v.name == obj.name).sort( (a, b) => new Date(b.time).getTime() - new Date(a.time).getTime() )
var index = data.map(v => v.time).indexOf(obj.time)
var num1 = Math.abs( new Date(data[index - 1].time).getTime() - new Date(obj.time).getTime() )
var num2 = Math.abs( new Date(data[index + 1].time).getTime() - new Date(obj.time).getTime() )
var res = num1 > num2 ? data[index + 1] : data[index - 1]
console.log(res) // 结果
var val = 'A'
var data = jsonHistory.filter(v => v.name == val).sort( (a, b) => new Date(b.time).getTime() - new Date(a.time).getTime() ).splice(0,2)
console.log(data) // 结果