Home > Enterprise >  Filtering and restructuring array of objects
Filtering and restructuring array of objects

Time:01-20

What I've got is array of objects

 const incomeRows = [
  {
    group: "Deck 1",
    categories: [
      { category: "Deck Supplies", reportvalue: 100, transdate: "2020-11" },
      { category: "Deck Supplies", reportvalue: 200, transdate: "2020-11" },
      { category: "Deck Contractors", reportvalue: 300, transdate: "2020-11" },
      { category: "Deck Contractors", reportvalue: 400, transdate: "2020-12" },
      { category: "Deck Contractors", reportvalue: 500, transdate: "2020-12" }
    ]
  },
  {
    group: "Deck 2",
    categories: [
      { category: "Deck Supplies", reportvalue: 10, transdate: "2020-11" },
      { category: "Deck Supplies", reportvalue: 20, transdate: "2020-11" },
      { category: "Deck Contractors", reportvalue: 30, transdate: "2020-11" },
      { category: "Deck Contractors", reportvalue: 40, transdate: "2020-12" },
      { category: "Deck Contractors", reportvalue: 50, transdate: "2020-12" }
    ]
  }
];

What I need to create is:

const finalOutput = [
  {
    group: "Deck 1",
    categories: [
      {
        category: "Deck Supplies",
        "2020-11": 300
      },
      {
        category: "Deck Contractors",
        "2020-11": 300,
        "2020-12": 900
      }
    ],
    groupMonthlyIncomes: {
      "2020-11": 600,
      "2020-12": 900
    }
  },
  {
    group: "Deck 2",
    categories: [
      {
        category: "Deck Supplies",
        "2020-11": 30
      },
      {
        category: "Deck Contractors",
        "2020-11": 30,
        "2020-12": 90
      }
    ],
    groupMonthlyIncomes: {
      "2020-11": 60,
      "2020-12": 90
    }
  }
];

So the category is unique and it has total values for each month like "2020-11": 300 and each group has its total monthly values like

groupMonthlyIncomes: {
 "2020-11": 60,
 "2020-12": 90
}

What I've done so far is:

let formatedRows = incomeRows.map(el => (
  {
    group: el.group,
    categories: []
  }
));

let formatedCategories = incomeRows.map(el => (
  el.categories.map(cat => {
    return {
      category: cat.category,
      [cat.transdate.toString()]: cat.reportvalue
    }
  })
));

Can anybody let me know what should be the next step or point me into any other solution if there is a better way to do it?

CodePudding user response:

try this:

incomeRows.map((row) => {
  return {
    group: row.group,
    categories: row.categories.map((cat) => {
      return {
        category: cat.category,
        [cat.transdate]: cat.reportvalue,
      }
    }),
    groupMonthlyIncomes: row.categories.reduce((result, next) => {
      const key = next.transdate;
      result[key] = result[key] ? result[key]   next.reportvalue : next.reportvalue;
      return result;
    }, {}),
  }
});

CodePudding user response:

const incomeRows = [{
        group: "Deck 1",
        categories: [{
                category: "Deck Supplies",
                reportvalue: 100,
                transdate: "2020-11"
            },
            {
                category: "Deck Supplies",
                reportvalue: 200,
                transdate: "2020-11"
            },
            {
                category: "Deck Contractors",
                reportvalue: 300,
                transdate: "2020-11"
            },
            {
                category: "Deck Contractors",
                reportvalue: 400,
                transdate: "2020-12"
            },
            {
                category: "Deck Contractors",
                reportvalue: 500,
                transdate: "2020-12"
            }
        ]
    },
    {
        group: "Deck 2",
        categories: [{
                category: "Deck Supplies",
                reportvalue: 10,
                transdate: "2020-11"
            },
            {
                category: "Deck Supplies",
                reportvalue: 20,
                transdate: "2020-11"
            },
            {
                category: "Deck Contractors",
                reportvalue: 30,
                transdate: "2020-11"
            },
            {
                category: "Deck Contractors",
                reportvalue: 40,
                transdate: "2020-12"
            },
            {
                category: "Deck Contractors",
                reportvalue: 50,
                transdate: "2020-12"
            }
        ]
    }
];

const reorganization = () => {
    const attrs = ['category', 'transdate', 'reportvalue', 'categories', 'groupMonthlyIncomes'];
    return incomeRows.map(row => {
        const categories = [...new Map(row.categories.map(item => [item[attrs[0]], item])).keys()];
        const map = new Map([...new Map(row.categories.map(item => [item[attrs[0]], new Map()]))]);

        for (let i = 0; i < categories.length; i  ) {
            const _map = map.get(categories[i]);
            for (let j = 0; j < row.categories.length; j  ) {

                if (row.categories[j][attrs[0]] == categories[i]) {
                    const _mapKey = row.categories[j][attrs[1]];
                    const _mapVal = row.categories[j][attrs[2]];
                    _map.set(_mapKey, _map.has(_mapKey) ? _map.get(_mapKey)   _mapVal : _mapVal);
                }
            }
            _map.set(attrs[0], categories[i]);
            categories[i] = Object.fromEntries([..._map].map(
                m => m.flat()).map(a => ({
                [a[0]]: a[1]
            })).flatMap(Object.entries));
        }
        row[attrs[3]] = categories;

        let groupMonthlyIncomes = new Map();

        for (let i = 0; i < categories.length; i  ) {
            const categorie = categories[i];
            delete categorie[attrs[0]];
            for (const [key, value] of Object.entries(categories[i])) {
                groupMonthlyIncomes.set(key, groupMonthlyIncomes.has(key) ? groupMonthlyIncomes.get(key)   value : value);
            }
        }

        row[attrs[4]] = Object.fromEntries([...groupMonthlyIncomes].map(
            m => m.flat()).map(a => ({
            [a[0]]: a[1]
        })).flatMap(Object.entries));
        //console.log(groupMonthlyIncomes);

        return row;
    });
}

console.log(reorganization());

  •  Tags:  
  • Related