简体   繁体   English

用变化的数据构造多维对象

[英]Structuring a multi-dimensional object with varying data

I have three functions which create three objects, firstPayment , interimPayment and finalPayment 我有创建三个对象的三个函数, firstPaymentinterimPaymentfinalPayment

firstPayment will always have data. firstPayment将始终具有数据。 But interimPayment shouldn't always be used, same finalPayment . 但是, interimPayment应该总是使用finalPayment ,与finalPayment相同。

The signifier that interimPayment or finalPayment isn't needed is based on another variable numberOfPayments . interimPaymentfinalPayment基于另一个变量numberOfPayments

If numberOfPayments === 1 then only firstPayment will have data. 如果numberOfPayments === 1则只有firstPayment将具有数据。 If numberOfPayments === 2 then both firstPayment and finalPayment are needed. 如果numberOfPayments === 2那么firstPaymentfinalPayment都是必需的。 And finally if numberOfPayments > 2 then all three payment objects are needed. 最后,如果numberOfPayments > 2则需要所有三个支付对象。

I'm trying to work out how to add all these objects to another object paymentSchedule based on the numberOfPayments variable. 我正在尝试找出如何根据numberOfPayments变量将所有这些对象添加到另一个对象paymentSchedule

Here's my start on the code but I'm a bit lost! 这是我的代码入门,但我有点迷路!

  getPaymentSchedule(id) {
    const firstPayment = this.getFirstPayment(id);
    const numberOfPayments = this.getNumberOfPayments(id);

    if (numberOfPayments === 2) {
      const finalPayment = this.getFinalPayment(id);
    } else if (numberOfPayments > 2) {
      const interimPayments = this.getInterimPayments(id);
    }
  }

This is where I'm a bit stuck. 这是我有点卡住的地方。 I need to add the three objects firstPayment , getInterimPayments and finalPayment in that order, but obviously only if they have been set. 我需要finalPayment顺序添加三个对象firstPaymentgetInterimPaymentsfinalPayment ,但显然只有在已设置它们的情况下。 I feel like I could do this some quicker way than lots and lots of if statements. 我觉得我可以比许多if语句更快地执行此操作。

Example of paymentSchedule I am trying to achieve: 我正在尝试实现的paymentSchedule示例:

{
    firstPayment: {
        label: "foo",
        amount: 123
    },
    interimPayment: {
        label: "foo",
        amount: 123
    },
    finalPayment: {
        label: "foo",
        amount: 123
    },
}

or

{
    firstPayment: {
        label: "foo",
        amount: 123
    },
    finalPayment: {
        label: "foo",
        amount: 123
    },
}

Maybe I'm missing something but is this what you're looking for? 也许我想念什么,但这是您要寻找的吗?

getPaymentSchedule(id) {
    const numberOfPayments = this.getNumberOfPayments(id);
    const schedule = {};
    schedule.firstPayment = this.getFirstPayment(id);

    if (numberOfPayments === 2) {
        schedule.finalPayment = this.getFinalPayment(id);
    } else if (numberOfPayments > 2) {
        schedule.interimPayments = this.getInterimPayments(id);
    }
    return schedule;
}

You could use an array with objects. 您可以将数组与对象一起使用。 The array reflects the order and the objects. 该数组反映了顺序和对象。

[
    {                              // exists always
        name: 'firstPayment',
        label: 'foo',
        amount: 123            
    },
    {                              // numberOfPayments > 2
        name: 'interimPayment',
        label: 'foo',
        amount: 123            
    },
    {                              // numberOfPayments > 1
        name: 'finalPayment',
        label: 'foo',
        amount: 123            
    }
]

To generate, you could use something like this 要生成,可以使用类似这样的东西

var schedule = [].concat(
        this.getFirstPayment(id)
        numberOfPayments > 2 ? this.getInterimPayments(id) : [],
        numberOfPayments > 1 ? this.getFinalPayment(id) : []
    );

This generates an array with the wanted payment date. 这将生成一个包含所需付款日期的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM