Typescript 如何使用lodash查找时间戳最接近的记录?

Typescript 如何使用lodash查找时间戳最接近的记录?,typescript,filter,timestamp,lodash,Typescript,Filter,Timestamp,Lodash,我是lodash的新手,希望使用它来查找数据集中包含最接近给定时间戳/x值的时间戳的对象的索引 假设给我一个时间戳var timestamp=“2019-01-01:01:30” 我的数据集如下所示: dataset = [{ x: "2019-01-01 00:01:38", y: 1 }, { x: "2019-01-01 01:01:39", y: 5 }, { x: "2019-01-01 02:01:40", y: 4

我是lodash的新手,希望使用它来查找数据集中包含最接近给定时间戳/x值的时间戳的对象的索引

假设给我一个时间戳
var timestamp=“2019-01-01:01:30”

我的数据集如下所示:

dataset = [{
    x: "2019-01-01 00:01:38",
    y: 1
  },
  {
    x: "2019-01-01 01:01:39",
    y: 5
  },
  {
    x: "2019-01-01 02:01:40",
    y: 4
  },
  {
    x: "2019-01-01 03:01:41",
    y: 1
  }
]
我希望它返回给我:数据集的索引,其中包含最接近的时间戳(在本例中是1,因为索引1处的时间戳最接近)

我希望它返回索引处记录的y值,该索引包含最接近的timestamp/x值(即5)。返回整个记录也会起作用。只是为了让我能得到y值

哪种lodash方法是实现这一目标的理想方法,我将如何着手构建它

使用.filter或.find的东西,也许

_.find(dataset, function(item) {
  return ...?
  );
})

非常感谢您的投入

您可以使用
Array.reduce()
或lodash的
\u.reduce()
迭代数组,并找到
x
时间戳之间的差异。存储差异和
y
值(如果较低)。对结果进行分解以获得
y

计算
diff
,取解析值之间差值的绝对值

const timestamp=“2019-01-01:01:30”
常量数据集=[{“x”:“2019-01-01 00:01:38”,“y”:1},{“x”:“2019-01-01:01:39”,“y”:5},{“x”:“2019-01-01 02:01:40”,“y”:4},{“x”:“2019-01-01 03:01:41”,“y”:1}]
const[y]=dataset.reduce((r,{x,y})=>{
const diff=Math.abs(Date.parse(timestamp)-Date.parse(x))
返回差值console.log(y)
您可以使用
Array.reduce()
或lodash的
\u.reduce()
迭代数组,并找到
x
时间戳之间的差异。存储差异和
y
值(如果较低)。对结果进行分解以获得
y

计算
diff
,取解析值之间差值的绝对值

const timestamp=“2019-01-01:01:30”
常量数据集=[{“x”:“2019-01-01 00:01:38”,“y”:1},{“x”:“2019-01-01:01:39”,“y”:5},{“x”:“2019-01-01 02:01:40”,“y”:4},{“x”:“2019-01-01 03:01:41”,“y”:1}]
const[y]=dataset.reduce((r,{x,y})=>{
const diff=Math.abs(Date.parse(timestamp)-Date.parse(x))
返回差值log(y)
您可以找到这样的时间戳。我假设时间戳不在同一天

const currentDate = new Date('2019-01-01 01:01:30');
const currentSeconds = (currentDate.getHours() * 60) + (currentDate.getMinutes() * 60) + currentDate.getSeconds();
const dates = _.map(dataset, set => {
    const date = new Date(set.x);
    if (date.getDate === currentDate.getDate && date.getMonth === currentDate.getMonth && date.getFullYear === date.getFullYear) {
        const setInSeconds = (date.getHours() * 60) + (date.getMinutes() * 60) + date.getSeconds();

        return {
            x: set.x,
            y: set.y,
            difference: Math.abs(setInSeconds - currentSeconds)
        };
    }
});
const closestDate = _.minBy(dates, 'difference');
console.log(closestDate);

你可以找到这样的时间戳。我假设时间戳不在同一天

const currentDate = new Date('2019-01-01 01:01:30');
const currentSeconds = (currentDate.getHours() * 60) + (currentDate.getMinutes() * 60) + currentDate.getSeconds();
const dates = _.map(dataset, set => {
    const date = new Date(set.x);
    if (date.getDate === currentDate.getDate && date.getMonth === currentDate.getMonth && date.getFullYear === date.getFullYear) {
        const setInSeconds = (date.getHours() * 60) + (date.getMinutes() * 60) + date.getSeconds();

        return {
            x: set.x,
            y: set.y,
            difference: Math.abs(setInSeconds - currentSeconds)
        };
    }
});
const closestDate = _.minBy(dates, 'difference');
console.log(closestDate);

有趣!在这种情况下,“r”代表记录还是索引?基本上是累加器。读一些关于reduce的文章。很有趣!在这种情况下,“r”代表记录还是索引?基本上是累加器。阅读一些关于reduce的文章。