Skip to content
Advertisement

How can I join an object with arrays into a single array using javascript?

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];
    }));
});

Advertisement

Answer

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; }
Advertisement