再次发生在我身上:D 我有一些问题,无法解决。
A有两个对象数组。两者都缺少信息。我想合并它们,但有一些规则:
首先是我的对象数组
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
值,并将其插入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:
项目代码。附:用于调试的Console.log
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
答案 0 :(得分:1)
任务解决了!我很高兴! Answer
Array({ ...input2[0], componentModel: _.assignWith({}, input2[0].componentModel, input1[0].componentModel, (t, s) => t ? t : s)})