概述
var arr = [
{datakey: "a", item: "28日19:27:33", scaleValue: "0"},
{datakey: "b", item: "28日19:27:33", scaleValue: "1"},
{datakey: "a", item: "28日19:28:00", scaleValue: "3"},
{datakey: "b", item: "28日19:28:00", scaleValue: "1"},
{datakey: "c", item: "28日19:28:00", scaleValue: "3"}
]
//补全后
{"a":["3","3"],"b":["1","1"],"c":[null,"0"]}
let keyDataMap = {};
let timeArr = [];
function generateDataForEchar(dataArr) {
let currentTimeData = {
"timeStamp": "",
"timeStampData": {}
}
for (let i = 0; i < dataArr.length; i++) {
let data = dataArr[i];
let timeStamp = data["item"];
let timeStampData;
if (!!currentTimeData.timeStamp && currentTimeData.timeStamp === timeStamp) {
//表示当前的数据仍是同一个时间节点,就先缓存
timeStampData = currentTimeData["timeStampData"];
} else {
//表示当前出现了一个新的时间点,此时需要将上一个时间点的数据存入各个数组中
addDataToArr(currentTimeData);
//重置时间节点的数据
timeStampData = {}
currentTimeData = {
"timeStamp": timeStamp,
timeStampData: timeStampData
}
}
//对于同个时间点的同个key的逻辑,目前是取最后一次出现的,你可以根据实际需要进行调整
timeStampData[data.datakey] = data.scaleValue;
}
//避免丢失最后一个时间点节点的数据
addDataToArr(currentTimeData);
}
function addDataToArr(currentTimeData) {
let timeStamp = currentTimeData.timeStamp;
if (!!timeStamp) {
timeArr.push(timeStamp);
let timeStampData = currentTimeData.timeStampData;
let tmpKeyMap = {};
for (let dataKey in timeStampData) {
let dataArr = keyDataMap[dataKey];
let timeStampValue = timeStampData[dataKey];
tmpKeyMap[dataKey] = "1";
if (!dataArr) {
dataArr = getArrayWithNull(timeArr.length - 1);
keyDataMap[dataKey] = dataArr;
}
keyDataMap[dataKey].push(timeStampValue)
}
for (let dataKey in keyDataMap) {
if (!tmpKeyMap[dataKey]) {
//当前节点内未出现对应的dataKey,则对应节点加入一个null
keyDataMap[dataKey].push(null);
}
}
}
}
function getArrayWithNull(size) {
let arr = []
for (let i = 0; i < size; i++) {
arr.push(null);
}
return arr;
}
generateDataForEchar(arr);
console.log(timeArr);
console.log(JSON.stringify(keyDataMap));
var arr = [
{datakey: "a", item: "28日19:27:33", scaleValue: "0"},
{datakey: "b", item: "28日19:27:33", scaleValue: "1"},
{datakey: "a", item: "28日19:28:00", scaleValue: "3"},
{datakey: "b", item: "28日19:28:00", scaleValue: "1"},
{datakey: "c", item: "28日19:28:00", scaleValue: "3"}
]
// 补全后
[
{datakey: "a", item: "28日19:27:33", scaleValue: "0"},
{datakey: "b", item: "28日19:27:33", scaleValue: "1"},
{datakey: "c", item: "28日19:27:33", scaleValue: null},
{datakey: "a", item: "28日19:28:00", scaleValue: "3"},
{datakey: "b", item: "28日19:28:00", scaleValue: "1"},
{datakey: "c", item: "28日19:28:00", scaleValue: "3"}
]
//提取 datakey
var keys = []
arr.forEach(e => {
keys.push(e.datakey)
});
keys = new Set(keys);
//这部分是提取 同时间数据
var resData = arr;
let tempArr = [];
let Data = [];
for (let i = 0; i < resData.length; i++) {
if (tempArr.indexOf(resData[i].item) === -1) {
Data.push({
item: resData[i].item,
dataInfo: [resData[i]]
});
tempArr.push(resData[i].item);
} else {
for (let j = 0; j < Data.length; j++) {
if (Data[j].item == resData[i].item) {
Data[j].dataInfo.push(resData[i]);
break;
}
}
}
}
//这部分是补齐
Data.forEach((e, i) => {
if (e.dataInfo.length !== keys.length) {
keys.forEach(sd => {
if (findIndexByKeyValue(e.dataInfo, 'datakey', sd) != -1) {
// console.log('yes', sd)
} else {
// console.log('no', sd);
var obj = {
datakey: sd,
item: e.dataInfo[0].item,
scaleValue: null
}
e.dataInfo.push(obj)
}
});
}
});
// console.log('补齐', Data)
function findIndexByKeyValue(arr, key, valuetosearch) {
for (var i = 0; i < arr.length; i++) {
if (arr[i][key] == valuetosearch) {
return i;
}
}
return -1;
}
//提取, 压平
var arrN = []
Data.forEach(e => {
// console.log('eeee', e.dataInfo)
arrN.push(e.dataInfo)
});
arrN = arrN.flat()
console.log('最终', arrN)
最后
以上就是大意飞鸟为你收集整理的js 数组,数据补全的全部内容,希望文章能够帮你解决js 数组,数据补全所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复