gpt4 book ai didi

javascript - 如何根据日期过滤数据?

转载 作者:行者123 更新时间:2023-11-30 13:59:03 25 4
gpt4 key购买 nike

我有一个数组,

var data = [
{ id: 1, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2017-05-01]" },
{ id: 2, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2018-01-01]" },
{ id: 3, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2019-01-01]" },
{ id: 4, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2017-05-01]" },
{ id: 5, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2018-01-01]" },
{ id: 6, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2019-01-01]" },
{ id: 7, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2018-01-01]" },
{ id: 8, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2019-01-01]" },
{ id: 9, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2017-05-01]" }
];


data.forEach(function(result) {
console.log(result.jobCategoryWithFromDate);
});

在控制台中给出了上述结果..

在 foreach 中,我需要返回具有最新日期的 jobCategoryWithFromDate

所以预期的结果是,(与下面相同)..

ASSIST.. / Assistance [valid from 2019-01-01]
PROF.. / Professional [valid from 2019-01-01]
SEN.. / Senior [valid from 2019-01-01]

以上是预期的结果,并没有改变,只需要过滤结果并返回 jobCategoryWithFromDate 返回具有最新日期的唯一值..

尽管名称重复,但过滤器需要根据日期(最新)进行处理..

编辑:

我已经接受了答案,但后来我才知道它并不是在所有情况下都有效,很抱歉没有完全测试它..

不知道我的解释是否有误,但如果用户名相同,则需要根据日期进行过滤,否则需要显示具有任何日期的唯一用户。

例如:

    var data = [
{ id: 1, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2017-05-01]" },
{ id: 2, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2018-01-01]" },
{ id: 3, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2019-01-01]" },
{ id: 4, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2017-05-01]" },
{ id: 5, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2018-01-01]" },
{ id: 6, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2019-01-01]" },
{ id: 7, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2018-01-01]" },
{ id: 8, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2019-01-01]" },
{ id: 9, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2017-05-01]" },
{ id: 10, jobCategoryWithFromDate: "Jun.. / Junior [valid from 2018-01-01]" },
{ id: 11, jobCategoryWithFromDate: "Usr.. / User [valid from 2019-01-01]" },
{ id: 12, jobCategoryWithFromDate: "Man.. / Manager [valid from 2017-05-01]" }
];

function extractDate(string){
return string.match(/[0-9]+-[0-9]+-[0-9]+/g);
}

var result = data.reduce((newestDates, currentItem)=>{
if (newestDates.length===0) newestDates.push(currentItem);
else {
const dateDifference = new Date(extractDate(newestDates[0].jobCategoryWithFromDate)) - new Date(extractDate(currentItem.jobCategoryWithFromDate));

if (dateDifference===0) newestDates.push(currentItem); // push items with the same date as newest found
else if (dateDifference <= 0) newestDates = [currentItem]; // if item has newer date the original array gets replaced with one containing just this item
// ignore case dateDifference >= 0 because we do not need to do anything with dates older than the newest found
}
return newestDates;
},[]);

console.log(result);

在此代码段中查看最后三个值,它们具有不同的用户名但与之前记录的日期相同。

这里的预期结果是我需要获取所有最后三个记录以及以上三个记录,因为它们是不同的用户,例如

ASSIST.. / Assistance [valid from 2019-01-01]
PROF.. / Professional [valid from 2019-01-01]
SEN.. / Senior [valid from 2019-01-01]
Jun.. / Junior [valid from 2018-01-01]
Usr.. / User [valid from 2019-01-01]
Man.. / Manager [valid from 2017-05-01]

最佳答案

假设日期的格式是YYYY-MM-DD,您可以只从每个元素中提取包含日期的字符串的一部分,并从中创建日期对象

编辑:您似乎想要获取每个用户的最后日期,而不是仅仅获取具有最新日期的条目数组,编辑片段以匹配它(您可以编辑部分如果您想要区分用户的不同比较,则使用正则表达式提取名称)

var data = [
{ id: 1, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2017-05-01]" },
{ id: 2, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2018-01-01]" },
{ id: 3, jobCategoryWithFromDate: "ASSIST.. / Assistance [valid from 2019-01-01]" },
{ id: 4, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2017-05-01]" },
{ id: 5, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2018-01-01]" },
{ id: 6, jobCategoryWithFromDate: "PROF.. / Professional [valid from 2019-01-01]" },
{ id: 7, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2018-01-01]" },
{ id: 8, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2019-01-01]" },
{ id: 9, jobCategoryWithFromDate: "SEN.. / Senior [valid from 2017-05-01]" },
{ id: 10, jobCategoryWithFromDate: "Jun.. / Junior [valid from 2018-01-01]" },
{ id: 11, jobCategoryWithFromDate: "Usr.. / User [valid from 2019-01-01]" },
{ id: 12, jobCategoryWithFromDate: "Man.. / Manager [valid from 2017-05-01]" }
];

function extractNameNDate(string){
return [string.match(/^\w+/), string.match(/[0-9]+-[0-9]+-[0-9]+/g)];
}

var result = data.reduce((newestDates, currentItem)=>{
const [user, date] = extractNameNDate(currentItem.jobCategoryWithFromDate);
if (newestDates[user]) {
// compare dates for given user
const [storedUser, storedDate] = extractNameNDate(newestDates[user].jobCategoryWithFromDate);
const dateDifference = new Date(storedDate) - new Date(date);

if (dateDifference <= 0) newestDates[user] = currentItem;

} else {
newestDates[user]=currentItem;
}
return newestDates;
},{});

result = Object.values(result);

console.log(result);

这是一个经过编辑的版本,用于匹配字符串的整个第一部分,直到 [] 大括号

var data = [
{ id: 1, jobCategoryWithFromDate: "01-05 Data Specialist [valid from 2019-01-01]" },
{ id: 2, jobCategoryWithFromDate: "01-05 Support [valid from 2019-01-01]" },
{ id: 3, jobCategoryWithFromDate: "03-04 Technician [valid from 2019-01-01]" },
{ id: 4, jobCategoryWithFromDate: "05 Engineer [valid from 2019-01-01]" },
{ id: 5, jobCategoryWithFromDate: "05 Technician [valid from 2019-01-01]" },
{ id: 6, jobCategoryWithFromDate: "05 Technologist [valid from 2019-01-01]" },
{ id: 7, jobCategoryWithFromDate: "06 Engineer [valid from 2019-01-01]" },
{ id: 8, jobCategoryWithFromDate: "06-07 Sr. Technician [valid from 2019-01-01]" },
{ id: 9, jobCategoryWithFromDate: "Intern [valid from 2019-01-01]" },
{ id: 10, jobCategoryWithFromDate: "06-07 Support [valid from 2019-01-01]" },
{ id: 11, jobCategoryWithFromDate: "06-07 Technologist [valid from 2019-01-01]" },
{ id: 12, jobCategoryWithFromDate: "07 Engineer [valid from 2019-01-01]" },
{ id: 13, jobCategoryWithFromDate: "08 Senior [valid from 2019-01-01]" },
{ id: 14, jobCategoryWithFromDate: "08-09 Support [valid from 2019-01-01]" },
{ id: 15, jobCategoryWithFromDate: "09 Senior [valid from 2019-01-01]" },
{ id: 16, jobCategoryWithFromDate: "10-11 Principal [valid from 2019-01-01]" },
{ id: 17, jobCategoryWithFromDate: "12-13 Sr. Principal [valid from 2019-01-01]" },
{ id: 18, jobCategoryWithFromDate: "14 Vice President [valid from 2019-01-01]" },
{ id: 19, jobCategoryWithFromDate: "15 President [valid from 2019-01-01]" }
];

function extractNameNDate(string){
return [string.match(/[^\/]*/), string.match(/[0-9]+-[0-9]+-[0-9]+/g)];
}

var result = data.reduce((newestDates, currentItem)=>{
const [user, date] = extractNameNDate(currentItem.jobCategoryWithFromDate);
if (newestDates[user]) {
// compare dates for given user
const [storedUser, storedDate] = extractNameNDate(newestDates[user].jobCategoryWithFromDate);
const dateDifference = new Date(storedDate) - new Date(date);

if (dateDifference <= 0) newestDates[user] = currentItem;

} else {
newestDates[user]=currentItem;
}
return newestDates;
},{});

result = Object.values(result);

console.log(result);

关于javascript - 如何根据日期过滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56700357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com