英文:
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
- 遍历数据
- 将
PartialRedemptionDate
转换为今天的日期 - 检查是否小于我们记住的差异
- 根据需要覆盖我们的标志
- 返回对象
- 将
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"
}
英文:
- Loop through the data
- Convert
PartialRedemptionDate
to todays date - Check if it's lower then our remembered diff
- Overweite our flag if needed
- Return the object
- Convert
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
<!-- end snippet -->
Returns:
{
"PartialRedemptionAmount": 2880000,
"PartialRedemptionDate": "2023-03-05T00:00:00Z",
"PartialRedemptionPrice": 100,
"PartialRedemptionType": "UnscheduledRedemption"
}
答案3
得分: 0
这里是JavaScript代码,我不会翻译它,但我可以帮助你理解代码或回答相关问题。如果您有任何问题或需要进一步的信息,请随时提出。
英文:
Here:
function findClosestDateInFuture() {
const now = new Date();
return poc
.filter(({ PartialRedemptionDate }) => new Date(PartialRedemptionDate) >= now)
.reduce((smallestPocItem, pocItem) => {
if (!smallestPocItem) {
return pocItem;
}
const pocDate = new Date(pocItem.PartialRedemptionDate);
const smallestPocDate = new Date(smallestPocItem.PartialRedemptionDate);
return pocDate.getTime() < smallestPocDate
? pocItem
: smallestPocItem;
}, null);
}
function findClosestDateInPast() {
const now = new Date();
return poc
.filter(({ PartialRedemptionDate }) => new Date(PartialRedemptionDate) < now)
.reduce((smallestPocItem, pocItem) => {
if (!smallestPocItem) {
return pocItem;
}
const pocDate = new Date(pocItem.PartialRedemptionDate);
const smallestPocDate = new Date(smallestPocItem.PartialRedemptionDate);
return pocDate.getTime() > smallestPocDate
? pocItem
: smallestPocItem;
}, null);
}
function findClosestDate() {
return poc.reduce((nearestPocItem, pocItem) => {
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 < 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) => {
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论