删除句子中的多字符(角度 9)

我有这个 json 响应:


id: 30

tableName: "UserCredit"

keyValues: "{"Id":39}"

oldValues: "{"CoinLastUpdate":"2020-02-18T14:18:13.5155426+00:00","ScoreLastUpdate":"2020-02-18T14:18:13.5155429+00:00"}"

newValues: "{"CoinLastUpdate":"2020-02-18T14:18:15.7325823+00:00","ScoreLastUpdate":"2020-02-18T14:18:15.7325826+00:00"}"

auditType: "Update"

createdOnUtc: "2020-02-18T14:18:15.7338989Z"

createdByRefId: 39

我想找出两者之间的区别oldValues,newValues然后我编写以下代码:


setOldNewValue(item: DeiffrentModel): void {

let oldValue;

let newValue;

console.log(item.oldValues)

if (item.newValues !== null) {

  newValue = item.newValues.split(',');

}

if (item.oldValues !== null) {

  oldValue = item.oldValues.split(',');

}

for (let index = 0; index < newValue.length; index++) {

  let addModel = {} as DeifferModel;

  addModel.field = 'id';

  addModel.newValue = newValue[index];

  console.log(oldValue)

  if (oldValue !== undefined) {

    addModel.oldValue = oldValue[index]

  }

  this.differModel.push(addModel);

}

this.findDiffrent = _.difference(newValue, oldValue);

}


现在我有这个问题:


**** 我创建了一个值数组newValue,oldValue但它显示如下:


0:“CoinLastUpdate”:“2020-02-18T14:18:13.5155426+00:00”


1: "ScoreLastUpdate":"2020-02-18T14:18:13.5155429+00:00"}


但我只需要2020-02-18T14:18:13.5155429+00:00


我怎么解决这个问题?


天涯尽头无女友
浏览 119回答 2
2回答

米脂

您应该使用 JSON 结构而不是手动解析字符串。我还将使用 DeifferModel 作为 aclass而不是 an interface,因此您可以更轻松地初始化项目:// in a xxx.model.ts fileexport class DeifferModel {&nbsp; constructor(public field, public newValue, public oldValue) {}}// in your componentsetOldNewValue(item: DeiffrentModel): void {&nbsp; if (item.newValues) {&nbsp; &nbsp; newValue = JSON.parse(item.newValues);&nbsp; }&nbsp; if (item.oldValues) {&nbsp; &nbsp; oldValue = JSON.parse(item.oldValues);&nbsp; }&nbsp; Object.keys(newValue).foreach(key => {&nbsp; &nbsp; const addModel = new DeifferModel('id', newValue[key], oldValue[key]);&nbsp; &nbsp; this.differModel.push(addModel);&nbsp; });&nbsp; this.findDiffrent = _.difference(newValue, oldValue); // no use of "this.differModel" ?}

偶然的你

不是将它们存储为数组,而是将值解析为对象if (item.newValues !== null) {&nbsp; newValue = item.newValues.split(',');}if (item.oldValues !== null) {&nbsp; oldValue = item.oldValues.split(',');}到oldValue = JSON.parse(oldValues);newValue = JSON.parse(newValues);您可以像这样找到以毫秒为单位的差异:CoinLastUpdateDiff = Math.abs(new Date(newValue.CoinLastUpdate) - new Date(oldValue.CoinLastUpdate));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript