返回大于今天的最接近日期,从对象数组中选择

huangapple go评论75阅读模式
英文:

Return closest date that is more than today from array of objects

问题

我有一个对象数组如下),我正在尝试找出如何返回在对象数组中大于今天日期的最接近日期

英文:

I have an array of Objects (below) and I am trying to figure out how to return the closest day from an array of objects that is more than todays date.

const poc = [
  {
    PartialRedemptionAmount: 2880000,
    PartialRedemptionDate: '2022-10-01T00:00:00Z',
    PartialRedemptionPrice: 100,
    PartialRedemptionType: 'UnscheduledRedemption',
  },
  {
    PartialRedemptionAmount: 2880000,
    PartialRedemptionDate: '2023-03-03T00:00:00Z',
    PartialRedemptionPrice: 100,
    PartialRedemptionType: 'UnscheduledRedemption',
  },
  {
    PartialRedemptionAmount: 2880000,
    PartialRedemptionDate: '2023-03-09T00:00:00Z',
    PartialRedemptionPrice: 100,
    PartialRedemptionType: 'UnscheduledRedemption',
  },
];

The datapoint I am trying to compare isPartialRedemptionDate

I tried doing a some since I need the the return object back that matches the criteria.

const closestDay = poc.some(poc)

any thoughts?

答案1

得分: 0

使用筛选、排序和日期解析,您可以实现此目标。

筛选以获取大于今天的日期,然后排序以按最接近的顺序排列。

更改 .filter(obj => Date.parse(obj.PartialRedemptionDate) > Date.parse(today)) 中的表达式,以获取不同的条件,无论是过去还是未来。

const today = new Date().toISOString();

const closestDay = poc
  .filter(obj => Date.parse(obj.PartialRedemptionDate) > Date.parse(today))
  .sort((a, b) => Date.parse(a.PartialRedemptionDate) - Date.parse(b.PartialRedemptionDate))[0];

console.log(closestDay);
英文:

using filter, sort, and date.parse you can achieve this

filter to get dates that are more than today, and sort to sort by closest

change the expression in .filter(obj => Date.parse(obj.PartialRedemptionDate) > Date.parse(today)) to get different condition either in the past or the future

const today = new Date().toISOString();

const closestDay = poc
  .filter(obj => Date.parse(obj.PartialRedemptionDate) > Date.parse(today))
  .sort((a, b) => Date.parse(a.PartialRedemptionDate) - Date.parse(b.PartialRedemptionDate))[0];

console.log(closestDay);

答案2

得分: 0

  1. 遍历数据
    1. PartialRedemptionDate 转换为今天的日期
    2. 检查是否小于我们记住的差异
    3. 根据需要覆盖我们的标志
    4. 返回对象
const poc = [{PartialRedemptionAmount: 2880000, PartialRedemptionDate: '2022-10-01T00:00:00Z', PartialRedemptionPrice: 100, PartialRedemptionType: 'UnscheduledRedemption' }, {PartialRedemptionAmount: 2880000, PartialRedemptionDate: '2023-03-03T00:00:00Z', PartialRedemptionPrice: 100, PartialRedemptionType: 'UnscheduledRedemption' }, {PartialRedemptionAmount: 2880000, PartialRedemptionDate: '2023-03-05T00:00:00Z', PartialRedemptionPrice: 100, PartialRedemptionType: 'UnscheduledRedemption' }];

const getClosestPoc = (data) => {
    let closestTime  = Number.MAX_SAFE_INTEGER,
        closestIndex = -1,
        current = Date.now();
                 
    for (let o in data) {
        let obj = data[o];
        let diff = current - new Date(obj.PartialRedemptionDate).getTime();
        if (diff < closestTime) {
            closestTime = diff;
            closestIndex = o;
        }
    }
    return (closestIndex < 0) ? null : data[closestIndex];
};

const res = getClosestPoc(poc);
console.log(res);

返回:

{
  "PartialRedemptionAmount": 2880000,
  "PartialRedemptionDate": "2023-03-05T00:00:00Z",
  "PartialRedemptionPrice": 100,
  "PartialRedemptionType": "UnscheduledRedemption"
}
英文:
  1. Loop through the data
    1. Convert PartialRedemptionDate to todays date
    2. Check if it's lower then our remembered diff
    3. Overweite our flag if needed
    4. Return the object

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const poc = [{PartialRedemptionAmount: 2880000, PartialRedemptionDate: &#39;2022-10-01T00:00:00Z&#39;, PartialRedemptionPrice: 100, PartialRedemptionType: &#39;UnscheduledRedemption&#39;, }, {PartialRedemptionAmount: 2880000, PartialRedemptionDate: &#39;2023-03-03T00:00:00Z&#39;, PartialRedemptionPrice: 100, PartialRedemptionType: &#39;UnscheduledRedemption&#39;, }, {PartialRedemptionAmount: 2880000, PartialRedemptionDate: &#39;2023-03-05T00:00:00Z&#39;, PartialRedemptionPrice: 100, PartialRedemptionType: &#39;UnscheduledRedemption&#39;, }, ];

const getClosestPoc = (data) =&gt; {
    let closestTime  = Number.MAX_SAFE_INTEGER,
        closestIndex = -1,
             current = Date.now();
             
    for (let o in data) {
        let obj = data[o];
        let diff = current - new Date(obj.PartialRedemptionDate).getTime();
        if (diff &lt; closestTime) {
            closestTime = diff;
            closestIndex = o;
        }
    }
    return (closestIndex &lt; 0) ? null : data[closestIndex];
};

const res = getClosestPoc(poc);
console.log(res);

<!-- end snippet -->

Returns:

{
  &quot;PartialRedemptionAmount&quot;: 2880000,
  &quot;PartialRedemptionDate&quot;: &quot;2023-03-05T00:00:00Z&quot;,
  &quot;PartialRedemptionPrice&quot;: 100,
  &quot;PartialRedemptionType&quot;: &quot;UnscheduledRedemption&quot;
}

答案3

得分: 0

这里是JavaScript代码,我不会翻译它,但我可以帮助你理解代码或回答相关问题。如果您有任何问题或需要进一步的信息,请随时提出。

英文:

Here:

function findClosestDateInFuture() {
  const now = new Date();
  return poc
    .filter(({ PartialRedemptionDate }) =&gt; new Date(PartialRedemptionDate) &gt;= now)
    .reduce((smallestPocItem, pocItem) =&gt; { 
      if (!smallestPocItem) {
        return pocItem;
      } 

      const pocDate = new Date(pocItem.PartialRedemptionDate);
      const smallestPocDate = new Date(smallestPocItem.PartialRedemptionDate);
      return pocDate.getTime() &lt; smallestPocDate
        ? pocItem
        : smallestPocItem;
    }, null);
}

function findClosestDateInPast() {
  const now = new Date();
  return poc
    .filter(({ PartialRedemptionDate }) =&gt; new Date(PartialRedemptionDate) &lt; now)
    .reduce((smallestPocItem, pocItem) =&gt; { 
      if (!smallestPocItem) {
        return pocItem;
      } 

      const pocDate = new Date(pocItem.PartialRedemptionDate);
      const smallestPocDate = new Date(smallestPocItem.PartialRedemptionDate);
      return pocDate.getTime() &gt; smallestPocDate
        ? pocItem
        : smallestPocItem;
    }, null);
}

function findClosestDate() {
  return poc.reduce((nearestPocItem, pocItem) =&gt; { 
    if (!nearestPocItem) {
      return pocItem;
    } 

    const pocDateDiff = Math.abs(new Date(pocItem.PartialRedemptionDate).getTime() - Date.now());
    const nearestPocDateDiff = Math.abs(new Date(nearestPocItem.PartialRedemptionDate).getTime() - Date.now());
    return pocDateDiff &lt; nearestPocDateDiff
      ? pocItem
      : nearestPocItem;
  }, null);
}

答案4

得分: 0

你可以在循环中将时间与当前日期和最接近的日期进行比较,就像以下的示例一样。

function getClosetData() {
  let closest = null;

  poc.forEach((item) => {
    if (new Date(item.PartialRedemptionDate) > new Date()) {
      if (!closest) {
        closest = item;

        return;
      }

      if (
        new Date(item.PartialRedemptionDate) <
        new Date(closest.PartialRedemptionDate)
      ) {
        closest = item;
      }
    }
  });

  return closest
}
英文:

You can compare the time with the current date and the current closest date in a loop, like the following.

function getClosetData() {
  let closest = null;

  poc.forEach((item) =&gt; {
    if (new Date(item.PartialRedemptionDate) &gt; new Date()) {
      if (!closest) {
        closest = item;

        return;
      }

      if (
        new Date(item.PartialRedemptionDate) &lt;
        new Date(closest.PartialRedemptionDate)
      ) {
        closest = item;
      }
    }
  });

  return closest
}

huangapple
  • 本文由 发表于 2023年3月8日 17:51:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定