如何将数组中的日期转换为字符串? [angular4]

时间:2018-09-05 13:52:32

标签: angular typescript

我有一个包含dateInterview:Date的数组:

  public notes: Array<{ idAgreement: string, note: string, dateInterview: Date }> = [];

当我将此数组发送到服务器时,所有dateInterview的值都应转换为字符串。我该怎么办?

我能够在输入中使用onChange转换值,但是由于种种原因我不能这样做。

这就是我在代码中所做的:

this.defaultAgreement.interviews = this.notes;

我将笔记分配给defaultAgreement.interviews     包含它的对象里面是哪个:

...,
public interviews: Array<any>,
....,

然后我发送包含采访的大对象。

谢谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您可以使用Array.prototype.map

let newArray = notes.map(({ idAgreement, note, dateInterview }) => ({ idAgreement, note, dateInterview: dateInterview.toString() }));

答案 1 :(得分:1)

以下是我的评论,最快的是:

const notesWithStringDates = this.notes
  .map(note => ({ ...note, dateInterview: note.dateInterview.toISOString() }))

const notes = [
  { date: new Date(), id: 0 }
];

const mapped = notes
  .map(note => ({ ...note, date: note.date.toISOString() }))
  
console.log(mapped);

相关问题