代码之家  ›  专栏  ›  技术社区  ›  Ilya

合并两个对象数组

  •  -1
  • Ilya  · 技术社区  · 7 年前

    又是我:D 我有一些问题,解决不了。

    A有两个对象数组。两者都缺少信息。我想合并它们,但有一些规则:

    1. 我有嵌套对象
    2. 从第一个对象获取值并 如果第二个值等于 第二次归零

    第一个我的对象数组

    componentId:"ExchangeCurrency"
    componentModel:"{"CurrencyRate":0.021362956633198035,"DebitAccount":null,"CreditAccount":null,"PurchaseAmount":"42.73","PaymentAmount":"2000","CommissionResult":{}}"
    

    第二个

    componentId:"ExchangeCurrency"
    componentModel:"{"CurrencyRate":0.0,"DebitAccount":{"AccountId":134118,"TypeId":64,"CardId":64531,"CurrencyId":22,"CardPaymentSystemTypeId":1523,"Label":"Зарплатная++","AvailableOwnFunds":111105.83,"Overdraft":500000.0,"IsPaymentFromCard":true,"ContractId":233,"AccountCloseDate":"0001-01-01T00:00:00"},"PurchaseAmount":0.0,"PaymentAmount":0.0,"CreditAccount":null,"CommissionResult":null,"PaymentReason":null}"
    

    在第二个对象中,您可以看到“CurrencyRate”、“PurchaseAmount”和“PaymentAmount”键的重要性为0.0。这就是我的目标 CurrencyRate 第一个对象的值,并将其插入 货币兑换率 在第二个对象。

    在结束时,我想看到像这样的对象数组

    componentId:"ExchangeCurrency"
    componentModel:"{`"CurrencyRate":0.021362956633198035`,"DebitAccount":{"AccountId":134118,"TypeId":64,"CardId":64531,"CurrencyId":22,"CardPaymentSystemTypeId":1523,"Label":"Зарплатная++","AvailableOwnFunds":111105.83,"Overdraft":500000.0,"IsPaymentFromCard":true,"ContractId":233,"AccountCloseDate":"0001-01-01T00:00:00"},`"PurchaseAmount":"42.73"`,`"PaymentAmount":"2000"`,"CreditAccount":null,"CommissionResult":null,"PaymentReason":null}"
    

    我写了几行代码,但它不适用于嵌套对象 Check JsFiddle pls

    任何帮助都将不胜感激!

    UPD:

    来自项目的代码。P、 美国控制台。调试日志

    if (this.props.location.state.formData.componentValues) {
            const formData = [];
            this.props.location.state.formData.componentValues.forEach((item) => {
              const newItem = {};
              newItem[item.componentId] = JSON.parse(item.componentModel);
              formData.push(newItem);
            });
            const formatedFormData = formData.reduce((p, c) => Object.assign(p, c), {});
            console.log(this.toComponentValues(formatedFormData), 'formatedFormData');
    
            if (localStorage.accountData) {
              const { debitAccountId } = JSON.parse(localStorage.accountData);
              service.GetPrefilledWizardPage({
                type: { formType: paymentType, ...formTypeParam },
                debitAccountId,
              })
                .then((data) => {
                  function customizer(objValue, srcValue) {
                    console.log(objValue, srcValue, 'customizer');
                    return !objValue ? srcValue : objValue;
                  }
    
                  const defaults = _.partialRight(_.assignWith, customizer);
                  console.log(data.paymentRequestRequisiteForm.paymentRequestRequisiteModel.componentValues[0]);
    
                  console.log(defaults({}, data.paymentRequestRequisiteForm.paymentRequestRequisiteModel.componentValues, this.toComponentValues(formatedFormData)));
                })
              localStorage.removeItem('accountData');
            }
    
            // this.loadPage({
            //   type: { formType: paymentType, ...formTypeParam },
            //   componentValues: here I want to pass my merged array of objects,
            // }, extraComponentConfig);
          }
    

    UPD2:也没用:( JSFiddle

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ilya    7 年前

    任务解决了!我很高兴! Answer

    Array({ ...input2[0],  componentModel: _.assignWith({}, input2[0].componentModel, input1[0].componentModel, (t, s) => t ? t : s)})