Filter out a object whose date property is not after current date.

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

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: &quot;9999-12-31&quot;,
  },
  {
    expirationDate: &quot;9999-12-31&quot;,
  },
  {
    expirationDate: &quot;9999-12-31&quot;,
  },
  {
    expirationDate: &quot;9999-12-31&quot;,
  },
  {
    expirationDate: &quot;9999-12-31&quot;,
  },
  {
    expirationDate: &quot;1999-12-31&quot;,
  },
];

	dateOfObjects.filter((el) =&gt; {
	  return moment.utc(el.expirationDate).isAfter(moment()) === true;
	});

I tried to console this before the return

console.log(&#39;moment.utc(el.expirationDate).isAfter(moment()) &#39;, 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&lt;StdDateStatus[]&gt; {
		let dateStatusEntities: StdDateStatusEntity[] = null;
		dateStatusEntities = await this.dbService.getConnection().getRepository(StdDateStatusEntity).find();

		dateStatusEntities.filter((el) =&gt; {
			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) =&gt; {
  return moment.utc(el.expirationDate).isAfter(moment()) === true;
});

console.log(filteredObject);

filteredObject will be the variable you want.

huangapple
  • 本文由 发表于 2023年5月18日 10:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277391.html
匿名

发表评论

匿名网友

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

确定