2

This is what I have (wealthByDistribution) and I require a solution like (expectedArray).

const wealthByDistribution = {
  CheckingAccount: [
    {
      "year": 2016,
      "month": 4,
      "value": 10
    },
    {
      "year": 2016,
      "month": 5,
      "value": 0
    },
    {
      "year": 2016,
      "month": 6,
      "value": 0
    },
    {
      "year": 2016,
      "month": 7,
      "value": 0
    }
  ],
  Company: [
    {
      "year": 2016,
      "month": 4,
      "value": 0
    },
    {
      "year": 2016,
      "month": 5,
      "value": 0
    },
    {
      "year": 2016,
      "month": 6,
      "value": 0
    },
    {
      "year": 2016,
      "month": 7,
      "value": 110
    }
  ],
  InvestmentAccount: [
    {
      "year": 2016,
      "month": 4,
      "value": 0
    },
    {
      "year": 2016,
      "month": 5,
      "value": 0
    },
    {
      "year": 2016,
      "month": 6,
      "value": 0
    },
    {
      "year": 2016,
      "month": 7,
      "value": 220
    }
  ],
  InvestmentInsurance: [
    {
      "year": 2016,
      "month": 4,
      "value": 0
    },
    {
      "year": 2016,
      "month": 5,
      "value": 0
    },
    {
      "year": 2016,
      "month": 6,
      "value": 0
    },
    {
      "year": 2016,
      "month": 7,
      "value": 330
    }
  ],
  Loan: [
    {
      "year": 2016,
      "month": 4,
      "value": 0
    },
    {
      "year": 2016,
      "month": 5,
      "value": 0
    },
    {
      "year": 2016,
      "month": 6,
      "value": 0
    },
    {
      "year": 2016,
      "month": 7,
      "value": 0
    }
  ],
  PassionAssets: [
    {
      "year": 2016,
      "month": 4,
      "value": 0
    },
    {
      "year": 2016,
      "month": 5,
      "value": 0
    },
    {
      "year": 2016,
      "month": 6,
      "value": 0
    },
    {
      "year": 2016,
      "month": 7,
      "value": 0
    }
  ]
}

const returnExpectedArray = (wealthByDistribution) => {
    const expectedArray = []
    return expectedArray
}

const expectedArray = [
    {
      "year": 2016,
      PassionAssets: 0,
      Loan: 0,
      InvestmentInsurance: 0,
      InvestmentAccount: 0,
      CheckingAccount: 10,
      Company: 0,
      "month": 4,
      "value": 0
    },
    {
      "year": 2016,
      PassionAssets: 0,
      Loan: 0,
      InvestmentInsurance: 0,
      InvestmentAccount: 0,
      CheckingAccount: 0,
      Company: 0,
      "month": 5,
      "value": 0
    },
    {
      "year": 2016,
      PassionAssets: 0,
      Loan: 0,
      InvestmentInsurance: 0,
      InvestmentAccount: 0,
      CheckingAccount: 0,
      Company: 0,
      "month": 6,
      "value": 0
    },
    {
      "year": 2016,
      PassionAssets: 0,
      Loan: 0,
      InvestmentInsurance: 330,
      InvestmentAccount: 220,
      CheckingAccount: 0,
      Company: 110,
      "month": 7,
      "value": 0
    }
]

Please if anyone can help me, I have been trying to solve it out for quite some time. I tried the following code, but it did not work as expected.

const wealthByDistributionKeys = Object.keys(wealthByDistribution);
const [ key, ...rest ] = wealthByDistributionKeys;
const firstArray = wealthByDistribution[key] || [];

const expectedArray = firstArray.map((item, i) => {
    item[key] = item.value;
    return Object.assign({}, item, ...rest.map(r => {
        wealthByDistribution[r][i][r] = wealthByDistribution[r][i].value;
        return wealthByDistribution[r][i];
    }));
});

4
  • 1
    What's the reasoning behind the Company property value? The last object is expected to be Company: 0, "month": 7, "value": 110, is that really what you want? I'd have thought Company: 110, "month": 7, "value": 0 would be more inline with the rest of the logic you appear to be doing (also, where does the 'value' property value come from in the expected output objects?) Commented Apr 24, 2021 at 20:40
  • Yes you are rigth, Company: 110, "month": 7, "value": 0. My error structuring the expected result. Commented Apr 24, 2021 at 21:17
  • What does the "value": 0 come from, then? Commented Apr 24, 2021 at 21:17
  • The value doesn't matter to me. I just need to concatenate the arrays and assign each property of the object to them as its value For example const wealthByDistribution = { CheckingAccount: [ { "year": 2016, "month": 4, "value": 10 } ], Company: [ { "year": 2016, "month": 4, "value": 0 } ] } Expected result const expected = [ { "year": 2016, CheckingAccount: 10, Company: 0, "month": 4, "value": 10 } ] Commented Apr 24, 2021 at 21:28

1 Answer 1

1

By using corresponding keys, you could collect all value with year/month and get a combined result.

const
    wealthByDistribution = { CheckingAccount: [{ year: 2016, month: 4, value: 10 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 0 }], Company: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 110 }], InvestmentAccount: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 220 }], InvestmentInsurance: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 330 }], Loan: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 0 }], PassionAssets: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 0 }] },
    result = Object.values(Object
        .entries(wealthByDistribution)
        .reduce((r, [k, a]) => {
            a.forEach(({ year, month, value }) => {
                const key = [year, month].join('|');
                r[key] ??= { year, month };
                r[key][k] = value;
            });
            return r;
        }, {})
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

3 Comments

This results in "Company": 110 in the last object, and no value in the output objects. I don't understand what OP is trying to do with those.
i don't either.
Thank you very much, this is just what I needed. You are amazing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.