从数组获取最新值

时间:2019-06-18 12:55:06

标签: javascript

在我的应用程序中,我需要获取任意数量的值中的最新值,并且获取最新的日期是关键因素。

const data = [
'PROF.. / Professional [valid from 2018-01-01]',
'PROF.. / Professional [valid from 2019-01-01]',
'PROF.. / Professional [valid from 2017-05-01]',
'PRIN.. / Principal [valid from 2018-01-01]',
'PRIN.. / Principal [valid from 2017-05-01]',
'PRIN.. / Principal [valid from 2019-05-01]'
]

console.log(data);

如何从数组中以PROF.. / Professional [valid from 2019-01-01]PRIN.. / Principal [valid from 2019-05-01]的形式获取结果,这是与他人进行比较的最新数据?

我现在需要获取两个值。.但是我的实际应用程序有100多个不同的名称(重复的)和日期(不同的)。

我有多个这样的用户,并且重复了多次,但是日期会有所不同,我该如何过滤名称为最新日期的结果?

4 个答案:

答案 0 :(得分:2)

您可以快速排序,然后循环遍历以查找密钥的第一个匹配项。

const data = [
'PROF.. / Professional [valid from 2018-01-01]',
'PROF.. / Professional [valid from 2019-01-01]',
'PROF.. / Professional [valid from 2017-05-01]',
'PRIN.. / Principal [valid from 2018-01-01]',
'PRIN.. / Principal [valid from 2017-05-01]',
'PRIN.. / Principal [valid from 2019-05-01]'
]

//match the parts to get the role and date
var re = /(.*)\s\[valid from (\d{4}-\d{2}-\d{2})\]/
var latest = 
  data
    .sort()  // sort it so they are in order
    .reverse()  // reverse it so the latest date is first
    .reduce((obj, item) => { // find the first keys
      const [match, key, date] = item.match(re)  // get the parts from reg exp match
      if (!obj[key]) obj[key] = date;  // if we do not have the key let store it
      return obj; // return the object for reduce
    }, {})


console.log(latest)

如果您不想排序,则可以比较日期字符串

const data = [
'PROF.. / Professional [valid from 2018-01-01]',
'PROF.. / Professional [valid from 2019-01-01]',
'PROF.. / Professional [valid from 2017-05-01]',
'PRIN.. / Principal [valid from 2018-01-01]',
'PRIN.. / Principal [valid from 2017-05-01]',
'PRIN.. / Principal [valid from 2019-05-01]'
]

//match the parts to get the role and date
var re = /(.*)\s\[valid from (\d{4}-\d{2}-\d{2})\]/
var latest = 
  data
    .reduce((obj, item) => { // find the first keys
      const [match, key, date] = item.match(re)  // get the parts from reg exp match
      if (!obj[key] || obj[key] < date) obj[key] = date;  // if we do not have the key let store it or if new date is grater
      return obj; // return the object for reduce
    }, {})


console.log(latest)

答案 1 :(得分:1)

  1. 您必须从字符串中解析日期(例如dataRow.split('valid from ')[1].split(']')[0]或使用正则表达式)
  2. 按日期排序
  3. 取第一个值

答案 2 :(得分:0)

如果行总是这样格式化,则使用子字符串获取从-11位置开始的10个字符,这将为您提供日期。之后,您必须比较每个日期并选择最新的日期。

答案 3 :(得分:0)

data = [
    'PROF.. / Professional [valid from 2018-01-01]',
    'PROF.. / Professional [valid from 2019-01-01]',
    'PROF.. / Professional [valid from 2017-05-01]',
    'PRIN.. / Principal [valid from 2018-01-01]',
    'PRIN.. / Principal [valid from 2017-05-01]',
    'PRIN.. / Principal [valid from 2019-05-01]'
    ]

//REGEX THAT MATCH DATE FROM STRING
const dateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;

reorderedIndexes = [];

//From data we extract dates and their indexes and store it to another array;
data.forEach((dataValue, index)=>{
    let dataValueDate = dataValue.match(dateRegex)[0];
    reorderedIndexes.push({index: index, date: dataValueDate});
});

//Then we sort temporary array by dates and map it so it will contain reordered indexes only
reorderedIndexes = reorderedIndexes.sort((a,b)=>{
    return new Date(b.date) - new Date(a.date);
}).map((d)=>{
    return d.index;
});

//At the end we use reorderedIndexes and based on them map the appropriate value from data array.
data = reorderedIndexes.map((reorderedIndex)=> data[reorderedIndex]);

console.log(data);

/*
RESULT
[ 'PRIN.. / Principal [valid from 2019-05-01]',
  'PROF.. / Professional [valid from 2019-01-01]',
  'PROF.. / Professional [valid from 2018-01-01]',
  'PRIN.. / Principal [valid from 2018-01-01]',
  'PROF.. / Professional [valid from 2017-05-01]',
  'PRIN.. / Principal [valid from 2017-05-01]' ]
*/