【问题标题】:Unable to figure out how to assert an object correctly无法弄清楚如何正确断言对象
【发布时间】:2019-03-12 05:24:57
【问题描述】:

let records = {
  2018: {
    Jan: {
      records: [{ 
        id: 123, 
        date: '01-Jan-2018', 
        amount: 300,
        type: 'expense'
       }],
      totalExpenses: 300,
      totalIncome: 0
    },
    Feb: {
      records: [{ 
        id: 234, 
        date: '01-Jan-2019', 
        description: 'Syabas', 
        amount: 100,
        type: 'income'
       }],
      totalExpenses: 0,
      totalIncome: 100
    }
  },
  2019: {
    Jan: {
      records: [{ 
        id: 789, 
        date: '01-Jan-2019', 
        description: 'McD', 
        amount: 150,
        type: 'expense'
       }],
      totalExpenses: 150,
      totalIncome: 0
    },
  }
}
let year = 2018;
let month = 'Jan';

const yearRecords = records[year] || { [year]: {} };
const monthRecords = yearRecords[month] || { [month]: {} };
const recordList = monthRecords['records'] || [];

const newRec = {id: 666, amount: 9999};
const newRecordList = [...recordList, newRec];
monthRecords['records'] = newRecordList;
monthRecords.totalExpenses = newRecordList.reduce((accumulator, record) => {
  return accumulator + record.amount;
}, 0);


console.log({...records, [year]: yearRecords})

我有一个现有records 的列表,我正在尝试将新记录newRec 附加到这个records 中。

挑战在于,年份的关键可能/可能不存在,几个月也是如此。如果存在,追加到records数组并增加totalExpenses字段。

如上所示,如果year/month 存在于records 中,我就可以正常工作,但如果我将它设置为let year = 2020,它会在记录中增加一层,这是错误的。我已经尝试了很多小时仍然无法弄清楚,想知道是否有更简单的方法来进行上述断言?

当年份 = 2020 时,下面将显示错误的结果

let records = {
      2018: {
        Jan: {
          records: [{ 
            id: 123, 
            date: '01-Jan-2018', 
            amount: 300,
            type: 'expense'
           }],
          totalExpenses: 300,
          totalIncome: 0
        },
        Feb: {
          records: [{ 
            id: 234, 
            date: '01-Jan-2019', 
            description: 'Syabas', 
            amount: 100,
            type: 'income'
           }],
          totalExpenses: 0,
          totalIncome: 100
        }
      },
      2019: {
        Jan: {
          records: [{ 
            id: 789, 
            date: '01-Jan-2019', 
            description: 'McD', 
            amount: 150,
            type: 'expense'
           }],
          totalExpenses: 150,
          totalIncome: 0
        },
      }
    }
    let year = 2020;
    let month = 'Jan';

    const yearRecords = records[year] || { [year]: {} };
    const monthRecords = yearRecords[month] || { [month]: {} };
    const recordList = monthRecords['records'] || [];

    const newRec = {id: 666, amount: 9999};
    const newRecordList = [...recordList, newRec];
    monthRecords['records'] = newRecordList;
    monthRecords.totalExpenses = newRecordList.reduce((accumulator, record) => {
      return accumulator + record.amount;
    }, 0);


    console.log({...records, [year]: yearRecords})

【问题讨论】:

  • 期望的行为是什么(例如,结果应该是什么样子)?
  • @naeramarth7:我添加了一段带有year=2020 的新代码,以表明我的实现有问题。在年份 2020 应该显示几个月,但它显示另一个 2020

标签: javascript object mutation


【解决方案1】:

您可以使用reduce 方法来做到这一点。

let records = {"2018":{"Jan":{"records":[{"id":123,"date":"01-Jan-2018","amount":300,"type":"expense"}],"totalExpenses":300,"totalIncome":0},"Feb":{"records":[{"id":234,"date":"01-Jan-2019","description":"Syabas","amount":100,"type":"income"}],"totalExpenses":0,"totalIncome":100}},"2019":{"Jan":{"records":[{"id":789,"date":"01-Jan-2019","description":"McD","amount":150,"type":"expense"}],"totalExpenses":150,"totalIncome":0}}}

let month = 'Jan';

function update(year, month, obj, target) {
  [year, month].reduce(function(r, e, i, a) {
    if(!a[i + 1] && r[e]) {
      r[e].records.push(obj);
      r[e].totalExpenses += obj.amount
    }
    return r[e] = (r[e] || (a[i + 1] ? {} : {
      records: [obj],
      totalExpenses: obj.amount,
      totalIncome: 0
    }))
  }, target)
}

update(2018, month, {id: 4, amount: 9999}, records);
update(2020, month, {id: 5, amount: 9999}, records);

console.log(records)

【讨论】:

  • 哇,我还不能写出这种优雅的解决方案。让我花一些时间来理解它。非常感谢!
猜你喜欢
  • 2019-10-26
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2023-03-28
  • 2014-12-18
  • 2015-11-03
  • 1970-01-01
相关资源
最近更新 更多