英文:
Moment.js Filter out a object whose date property is not after current date
问题
I am using moment.js in my predicate function for the filter method on an array of objects who each have an expiration_date
property.
我在我的筛选函数中使用moment.js处理具有expiration_date
属性的对象数组。
I would like to filter out the object whose expiration_date
property is older than today. In my sample array it would be the last one.
我想要筛选出expiration_date
属性早于今天的对象。在我的示例数组中,这将是最后一个对象。
let dateOfObjects = [
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "1999-12-31",
},
];
dateOfObjects = dateOfObjects.filter((el) => {
return moment.utc(el.expirationDate).isAfter(moment()) === true;
});
I tried to console this before the return
我尝试在返回之前将其输出到控制台
console.log('moment.utc(el.expirationDate).isAfter(moment()) ', moment.utc(el.expirationDate).isAfter(moment()) === true);
And it is logging true for all the last, but when I console.log dateOfObjects
I still see it?!
它对所有最后一个对象都记录true,但当我输出dateOfObjects
时仍然看到它?
What on earth am I doing wrong?
我到底做错了什么?
This example above is in an async function below...
上面的示例位于下面的异步函数中...
async getAllDateStatuses(): Promise<StdDateStatus[]> {
let dateStatusEntities: StdDateStatusEntity[] = null;
dateStatusEntities = await this.dbService.getConnection().getRepository(StdDateStatusEntity).find();
dateStatusEntities = dateStatusEntities.filter((el) => {
return moment.utc(el.expirationDate).isAfter(moment()) === true;
});
return this.populateMultiple(dateStatusEntities);
}
Should that matter?
这是否重要?
英文:
I am using moment.js in my predicate function for the filter method on a array of objects who each have an expiration_date
property.
I would like to filter out the object whose expiration_date
property is older than today. In my sample array it would be the last one.
let dateOfObjects = [
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "9999-12-31",
},
{
expirationDate: "1999-12-31",
},
];
dateOfObjects.filter((el) => {
return moment.utc(el.expirationDate).isAfter(moment()) === true;
});
I tried to console this before the return
console.log('moment.utc(el.expirationDate).isAfter(moment()) ', moment.utc(el.expirationDate).isAfter(moment()) === true);
And it is logging true for all the last, but when I console.log dateOfObjects
I still see it?!
What on earth am I doing wrong?
This example above is in a async function below...
async getAllDateStatuses(): Promise<StdDateStatus[]> {
let dateStatusEntities: StdDateStatusEntity[] = null;
dateStatusEntities = await this.dbService.getConnection().getRepository(StdDateStatusEntity).find();
dateStatusEntities.filter((el) => {
return moment.utc(el.expirationDate).isAfter(moment()) === true;
});
return this.populateMultiple(dateStatusEntities);
}
Should that matter?
答案1
得分: 1
Your dateOfObjects.filter()
works just fine for me.
You should know that
> The filter() method does not change the original array.
因此,即使在执行 dateOfObjects.filter()
之后,执行 console.log(dateOfObjects)
也会给您相同的值。
所以,要收集筛选后的数组,您需要将 dateOfObjects.filter()
定义为一个变量
示例:
const filteredObject = dateOfObjects.filter((el) => {
return moment.utc(el.expirationDate).isAfter(moment()) === true;
});
console.log(filteredObject);
filteredObject
将是您想要的变量。
英文:
Your dateOfObjects.filter()
works just fine for me.
You should know that
> The filter() method does not change the original array.
Source: Definition and Usage
Hence, doing console.log(dateOfObjects)
will give you the same value even though you did dateOfObjects.filter()
before it.
So, what you need to do to collect the filtered array is to define dateOfObjects.filter()
in a variable
Example:
const filteredObject = dateOfObjects.filter((el) => {
return moment.utc(el.expirationDate).isAfter(moment()) === true;
});
console.log(filteredObject);
filteredObject
will be the variable you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论