如何根据日期过滤数据?

时间:2019-06-21 09:14:23

标签: javascript

我有一个数组,

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]

5 个答案:

答案 0 :(得分:1)

假设日期的格式为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);

答案 1 :(得分:1)

我将首先找到最新日期,然后过滤具有该日期的对象的数组。这将仅返回日期最高的对象:

oncomplete

答案 2 :(得分:0)

您可以应用正则表达式从字符串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]" }
    ];

var result= data.map(d => ({...d, date: d.jobCategoryWithFromDate.match(/(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g)[0]}))..sort((a, b)=> new Date(b.date) - new Date(a.date))

console.log(result)

答案 3 :(得分:0)

您可以使用简单的reduce来实现, 您可以通过存储数组以及currentMaxDateTS来改善缩减率,而不是在每次迭代中重新计算时间戳。

如果您需要有关此代码段的更多解释,请不要犹豫

const 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]" }
    ];

// Function returning the date string inside  [valid from 2017-05-01]
const getDateFromJobCategory = (jobCategoryWithFromDate) =>  jobCategoryWithFromDate.match(/[0-9]+-[0-9]+-[0-9]+/g);


const filteredData = data.reduce((acc, data) => {
  // Get data date in timestamp
  const dataDateString = getDateFromJobCategory(data.jobCategoryWithFromDate);
  const dataDateTS = new Date(dataDateString).getTime();
  // Get current max date in timestamp
  const currentMaxDate = getDateFromJobCategory(acc[0]);
  const currentMaxDateTS = new Date(currentMaxDate).getTime()

  return dataDateTS > currentMaxDateTS ? [data.jobCategoryWithFromDate] : dataDateTS === currentMaxDateTS ? [...acc, data.jobCategoryWithFromDate] : acc

}, ["[valid from 0001-01-01]"]);

   const finalResult = filteredData.forEach(jobCategoryWithFromDate => 
   console.log(jobCategoryWithFromDate))

答案 4 :(得分:0)

您可以通过使用正则表达式提取日期来进行简单排序,然后使用递归函数来检查是否存在多个相同的日期:

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]" }
];

const getDate = date => +date.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/g)[0].split('-').join('')

data = data.sort(({jobCategoryWithFromDate:date1}, {jobCategoryWithFromDate:date2}) => getDate(date2) - getDate(date1))

const getRecent = (data, i=0) => getDate(data[i].jobCategoryWithFromDate) === getDate(data[i + 1].jobCategoryWithFromDate) ? [data[i], ...getRecent(data, i+1)] : [data[i]]

getRecent(data).forEach(obj => console.log(obj))