简体   繁体   English

比较两个对象数组以获得不同的结果

[英]Comparing Two arrays of objects to obtain different results

 accounts: [ 
  { 
    uuid: '47833371-753a-412a-a00c-b43fc48b662b',
    cash: 50000 
  }, { 
    uuid: 'b9a30e79-7dc6-45c7-b277-6f03b43f0f9a',
    cash: 40000 
  } 
],
participations: [ 
  { 
    product_id: '0ebe9585-9506-4e28-8ca1-561726d51e30',
    number_participations: 283,
    net_asset_value: '4.0000000000',
    id_account: '47833371-753a-412a-a00c-b43fc48b662b' 
  },{ 
    product_id: '658d56f9-37f6-44ea-b528-861068be4743',
    number_participations: 100,
    net_asset_value: '8.0000000000',
    id_account: '47833371-753a-412a-a00c-b43fc48b662b' 
  } 
] 
}

I have two arrays of objects, I want to go through both at the same time, and obtain the patrimony of each of them separately, and then, when listing all accounts, each one has its patrimony added to its object.我有两个对象数组,我想同时遍历两个对象,分别获取每个对象的属性,然后在列出所有帐户时,每个对象都将其属性添加到其对象中。

let patrimony = 0
for (let i = 0; i < participations.length; i++) {
    for (let j = 0; j < accounts.length; j++) {
        if (participations[i].uuid_account === accounts[j].uuid) {
            patrimony += participations[i].participations * parseFloat(participations[i].net_asset_value);
        }
    }
}

With my approach, what I am getting is simply the sum of the participations multiply for the asset value of the participation of both accounts.用我的方法,我得到的只是参与的总和乘以两个账户参与的资产价值。

"accounts": [
  {
    "uuid": "47833371-753a-412a-a00c-b43fc48b662b",
    "patrimony": 1932,

  },
  {
    "uuid": "b9a30e79-7dc6-45c7-b277-6f03b43f0f9a",
    "patrimony": 1932,
  }
]

What I want and I don't know how it's to get the patrimony of each account and add it to correspondent object.我想要什么,我不知道如何获取每个帐户的遗产并将其添加到通讯对象。 Thanks in advance.提前致谢。

I think there is a few errors.我认为有几个错误。 Please check the fix here.请在此处检查修复。

 let patrimony = 0 for (let i = 0; i < participations.length; i++) { for (let j = 0; j < accounts.length; j++) { // error if (participations[i].uuid_account === accounts[j].uuid) { if (participations[i].id_account === accounts[j].uuid) { // error patrimony += participations[i].participations * parseFloat(participations[i].net_asset_value); patrimony += participations[i].number_participations * parseFloat(participations[i].net_asset_value); } } }

The description of the problem doesn't exactly match the provided code, but I guess you could use a combination of map / filter / reduce to get your results.问题的描述与提供的代码不完全匹配,但我想您可以使用map / filter / reduce的组合来获得结果。 This is how I would do it, assuming accounts and participants are object keys of a data object:这就是我会怎么做,假设accountsparticipantsdata对象的对象键:

data.accounts = data.accounts.map(function (account) {
  account.patrimony = data.participations
    .filter(function (participation) {
      return participation.id_account === account.uuid;
    })
    .reduce(function (accumulator, currentValue) {
      return accumulator + (currentValue.number_participations * parseFloat(currentValue.net_asset_value));
    }, 0);

  return account;
});

Here's a link showing the full working code:这是一个显示完整工作代码的链接:

https://jsfiddle.net/fv7hb8oL/ https://jsfiddle.net/fv7hb8oL/

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

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