根据日期将对象数组筛选/减少为新对象



考虑以下对象数组:

const data = [
*{...more data from previous dates}*,
{
unixDate: 1650348034,  //yesterday
dateTime: Tue Apr 19 2022 23:00:34,
severityLevel: 1,
severity: "Light"
},
{
unixDate: 1650348034,  //yesterday
dateTime: Tue Apr 19 2022 14:00:34,
severityLevel: 3,
severity: "Moderate"
},
{
unixDate: 1650440700,  //today
dateTime: Wed Apr 20 2022 15:45:00,
severityLevel: 2,
severity: "Moderate-Light"
},
{
unixDate: 1650442500,  //today
dateTime: Wed Apr 20 2022 15:45:00,
severityLevel: 4,
severity: "Heavy"
},
{
unixDate: 1650427234,  //today
dateTime: Wed Apr 20 2022 12:00:00,
severityLevel: 3,
severity: "Moderate"
}
]

我想退回以下物品:

{
*///...records from previous dates,* 
1650348034 : 2, //yesterday record taking only one of the unixtimestamp, and the average value of 'severityLevel'.
1650440700 : 3  //same as above but unixtimestamp is today.
}

我使用dayjs包通过isToday插件来确定日期是否是今天,但我想不出如何比较日期。data每天都在增长,因为它记录了新的读数。我不太熟悉ES6中的数组过滤器/减少方法,它们在这里有用吗?感谢您的帮助!

获取一个唯一的天数列表,并根据计算的天数构建一个对象

const data = [ { unixDate: 1650348034, dateTime: "Tue Apr 19 2022 23:00:34", severityLevel: 1, severity: "Light" }, { unixDate: 1650348034, dateTime: "Tue Apr 19 2022 14:00:34", severityLevel: 3, severity: "Moderate" }, { unixDate: 1650440700, dateTime: "Wed Apr 20 2022 15:45:00", severityLevel: 2, severity: "Moderate-Light" }, { unixDate: 1650442500, dateTime: "Wed Apr 20 2022 15:45:00", severityLevel: 4, severity: "Heavy" }, { unixDate: 1650427234, dateTime: "Wed Apr 20 2022 12:00:00", severityLevel: 3, severity: "Moderate" } ]
//Get unique list of days
let unique = [... new Set(data.map(d => new Date(d.unixDate * 1000).toLocaleDateString("en-US")))];
//build object based on this list
let results = Object.fromEntries(unique.map(m => {
let records = data.filter(v => new Date(v.unixDate * 1000).toLocaleDateString("en-US") === m); 
return [records[0].unixDate, records.reduce((v,o) => v+=o.severityLevel, 0) / records.length ]
}));
console.log(results);

您正在寻找的方法是Array.map(),它非常容易使用,您在数组上调用它,并提供一个函数,该函数将为每个索引返回新数据,而原始数据将在函数自身时提供。

要执行您想要执行的操作,还必须使用Object.fromEntries()将数组转换为对象。以下是您的操作方法:

let newData = Object.fromEntries(data.map((d)=>[d.unixDate, d.severityLevel]));

这段代码将首先将您的数据转换为一个包含数组的数组,其中0索引是密钥,也就是时间戳,1索引是该密钥的数据,也就是您的严重性级别。它看起来像这样:

[
[1650348034,1],
[1650348034,3],
[1650440700,2],
[1650442500,4],
[1650427234,3]
]

然后它被转换为你的对象

点击此处获取更多信息:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries

希望这能帮助

首先需要将这些unix时间转换为javascriptDate对象。然后,您可以按年份/月份/日期进行分组,然后对结果进行平均。

const data = [   
{
unixDate: 1650348034,  //yesterday
dateTime: "Tue Apr 19 2022 23:00:34",
severityLevel: 1,
severity: "Light"
},
{
unixDate: 1650348034,  //yesterday
dateTime: "Tue Apr 19 2022 14:00:34",
severityLevel: 3,
severity: "Moderate"
},
{
unixDate: 1650440700,  //today
dateTime: "Wed Apr 20 2022 15:45:00",
severityLevel: 2,
severity: "Moderate-Light"
},
{
unixDate: 1650442500,  //today
dateTime: "Wed Apr 20 2022 15:45:00",
severityLevel: 4,
severity: "Heavy"
},
{
unixDate: 1650427234,  //today
dateTime: "Wed Apr 20 2022 12:00:00",
severityLevel: 3,
severity: "Moderate"
}
]
const x = Object.values(data.map(x => ({...x, dateTime: new Date(x.unixDate * 1000)}))
.reduce( (acc,i) => {
const key = "" + i.dateTime.getFullYear() + i.dateTime.getMonth() + i.dateTime.getDate();
acc[key] = acc[key] || [];
acc[key].push(i);
return acc
},{}))
.reduce( (obj,rec) => {
return {...obj, [rec[0].unixDate]: rec.reduce( (acc,i) => acc+i.severityLevel,0) / rec.length }
},{})
console.log(x)

注意,如果属性dateTime已经是一个javascript日期,则可以不使用data.map(x => ({...x, dateTime: new Date(x.unixDate * 1000)}))部分。

使用util方法dayBegins的另一种方法,它将计算一天的开始时间戳。这将有助于确定时间戳是否落在同一天。

除此之外,只需构建一个跟踪对象,其中键为dayBegin,值为severityLevel和number occurrences。

const dayBegins = (uDate) =>
"" + uDate * 1000 - ((uDate * 1000) % (24 * 60 * 60 * 1000));
const process = (arr, output = {}) => {
arr.forEach(({ unixDate, severityLevel }) => {
const key = dayBegins(unixDate);
if (key in output) {
output[key].count += 1;
output[key].sum += severityLevel;
} else {
output[key] = {
unixDate,
count: 1,
sum: severityLevel,
};
}
});
return Object.values(output).reduce(
(acc, { unixDate, sum, count }) =>
Object.assign(acc, { [unixDate]: sum / count }),
{}
);
};
const data = [
{
unixDate: 1650348034, //yesterday
dateTime: "Tue Apr 19 2022 23:00:34",
severityLevel: 1,
severity: "Light",
},
{
unixDate: 1650348034, //yesterday
dateTime: "Tue Apr 19 2022 14:00:34",
severityLevel: 3,
severity: "Moderate",
},
{
unixDate: 1650440700, //today
dateTime: "Wed Apr 20 2022 15:45:00",
severityLevel: 2,
severity: "Moderate-Light",
},
{
unixDate: 1650442500, //today
dateTime: "Wed Apr 20 2022 15:45:00",
severityLevel: 4,
severity: "Heavy",
},
{
unixDate: 1650427234, //today
dateTime: "Wed Apr 20 2022 12:00:00",
severityLevel: 3,
severity: "Moderate",
},
];
console.log(process(data))

好吧,我希望我能正确理解你。这是一个例子,说明你如何实现与你想要的输出类似的东西:

//input
const data = [
{
"unixDate": 1650348034,  //yesterday
"dateTime": "Tue Apr 19 2022 23:00:34",
"severityLevel": 1,
"severity": "Light"
},
{
unixDate: 1650348034,  //yesterday
dateTime: "Tue Apr 19 2022 14:00:34",
severityLevel: 3,
severity: "Moderate"
},
{
unixDate: 1650440700,  //today
dateTime: "Wed Apr 20 2022 15:45:00",
severityLevel: 2,
severity: "Moderate-Light"
},
{
unixDate: 1650442500,  //today
dateTime: "Wed Apr 20 2022 15:45:00",
severityLevel: 4,
severity: "Heavy"
},
{
unixDate: 1650427234,  //today
dateTime: "Wed Apr 20 2022 12:00:00",
severityLevel: 3,
severity: "Moderate"
}
];
// ES6 computed property syntax
const arr = data.map(x => { return { [x.unixDate]: x.severityLevel }});
//output
console.log(arr.reduce(function(result, current) {
return Object.assign(result, current);
}, {}));

相关内容

  • 没有找到相关文章

最新更新