简体   繁体   English

如何使用 ES6 Object spread 更新数组内的对象?

[英]How to use ES6 Object spread to update object inside array?

I've got the following array of objects which comes from a response:我有以下来自响应的对象数组:

const baseInput = [{
    PaymentRequirementsDetail:
    { dateDue: '12/02/2019',
        outstandingMinimum: { Money: { amount: '5.20', code: 'GBP' } },
        overlimit: { Money: { amount: '345.20', code: 'GBP' } },
        arrears: { Money: { amount: '345.20', code: 'GBP' } } }
},
{ Account: {},
    AccountId: '00000012345',
    CardBrand: 'SOMEBRAND',
    isAccountElibible: false,
    Customer:
    { salutation: 'Mr',
        givenName: 'James',
        familyName: 'Jamesy',
        suffix: 'Dr' },
    Delinquency: { monthsInArrears: 0, isOverlimit: true } }]

I am then transforming the response with a bunch of functions and am returning a friendly, formatted version of the above.然后我用一堆函数转换响应,并返回一个友好的、格式化的上述版本。

const baseOutput = transform(baseInput);

This returns:这将返回:

   {    name: 'Mr James Jamesy, Dr',
        cardBrand: 'SOMEBRAND',
        isAccountElibible: false,
        delinquency: { monthsInArrears: 0, isOverlimit: true },
        dateDue: '12/02/2019',
        outstandingMinimumAmount: 'GBP, 5.20',
        overlimitAmount: 'GBP, 345.20',
        arrearsAmount: 'GBP, 345.20' }

I would now like to test this and generate a few snapshots.我现在想对此进行测试并生成一些快照。

I can copy/paste the above code into my test-cases and change values as I do my assertions which works fine.我可以将上面的代码复制/粘贴到我的测试用例中,并在我做我的断言时更改值,效果很好。 Like this;像这样;

    test('should omit suffix if it is undefined', () => {
    const input = [{
        PaymentRequirementsDetail:
        { dateDue: '12/02/2019',
            outstandingMinimum: { Money: { amount: '5.20', code: 'GBP' } },
            overlimit: { Money: { amount: '345.20', code: 'GBP' } },
            arrears: { Money: { amount: '345.20', code: 'GBP' } } }
    },
    { Account: {},
        AccountId: '00000012345',
        CardBrand: 'SOMEBRAND',
        isAccountElibible: true,
        Customer:
        { salutation: 'Mr',
            givenName: 'James',
            familyName: 'Jamesy' },
        Delinquency: { monthsInArrears: 0, isOverlimit: true } }];

    const output = transform(input);

    expect(baseOutput).toMatchDiffSnapshot(output);
});

This will generate my snapshot as I require it and I will be able to see the difference between the version with a suffix and the version without one clearly.这将根据我的需要生成我的快照,我将能够清楚地看到带有后缀的版本和没有后缀的版本之间的区别。

However I believe that there is a cleaner way to do this using the object spread operator.但是我相信有一种更简洁的方法可以使用对象扩展运算符来做到这一点。 Instead of all of the above code, I should be left with;而不是上面的所有代码,我应该留下;

 const input = [{
        ...baseInput,
        Customer:
        { salutation: 'Mr',
        givenName: 'James',
        familyName: 'Jamesy'
        }
    }];

I am unable to however utilise the spread operator in a way so that I can achieve that.但是,我无法以某种方式利用扩展运算符来实现这一目标。 Can anyone see where my mistake is?谁能看到我的错误在哪里?

Your baseInput is an Array with two items.您的baseInput是一个包含两个项目的数组。 The spread operator works on either arrays or objects, what you are doing here is spreading the array into your target object.扩展运算符适用于数组或对象,您在这里所做的是将数组扩展到目标对象中。

If your model does not change, you could simply spread the indexed object into your target like so:如果您的模型没有改变,您可以简单地将索引对象传播到您的目标中,如下所示:

const input = [{
    ...baseInput[0]
  },{
        ...baseInput[1],
        Customer:
        { salutation: 'Mr',
        givenName: 'James',
        familyName: 'Jamesy'
        }
    }];

https://stackblitz.com/edit/typescript-imcqkh?file=index.ts https://stackblitz.com/edit/typescript-imcqkh?file=index.ts

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

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