为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

天涯尽头无女友 2022-05-14 15:21:02
我有这个 json 响应:id: 30tableName: "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我怎么解决这个问题?
查看完整描述

2 回答

?
米脂

TA贡献1836条经验 获得超3个赞

您应该使用 JSON 结构而不是手动解析字符串。

我还将使用 DeifferModel 作为 aclass而不是 an interface,因此您可以更轻松地初始化项目:


// in a xxx.model.ts file

export class DeifferModel {

  constructor(public field, public newValue, public oldValue) {}

}


// in your component

setOldNewValue(item: DeiffrentModel): void {

  if (item.newValues) {

    newValue = JSON.parse(item.newValues);

  }

  if (item.oldValues) {

    oldValue = JSON.parse(item.oldValues);

  }

  Object.keys(newValue).foreach(key => {

    const addModel = new DeifferModel('id', newValue[key], oldValue[key]);

    this.differModel.push(addModel);

  });

  this.findDiffrent = _.difference(newValue, oldValue); // no use of "this.differModel" ?

}


查看完整回答
反对 回复 2022-05-14
?
偶然的你

TA贡献1841条经验 获得超3个赞

不是将它们存储为数组,而是将值解析为对象


if (item.newValues !== null) {

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

}

if (item.oldValues !== null) {

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

}


oldValue = JSON.parse(oldValues);

newValue = JSON.parse(newValues);

您可以像这样找到以毫秒为单位的差异:


CoinLastUpdateDiff = Math.abs(new Date(newValue.CoinLastUpdate) - new Date(oldValue.CoinLastUpdate));



查看完整回答
反对 回复 2022-05-14
  • 2 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信