首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤和重构对象数组

过滤和重构对象数组
EN

Stack Overflow用户
提问于 2022-01-19 23:58:08
回答 3查看 67关注 0票数 0

我得到的是一系列的对象

代码语言:javascript
复制
 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" }
    ]
  }
];

我需要创造的是:

代码语言:javascript
复制
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
    }
  }
];

所以这个类别是唯一的,它有每个月的总价值,比如"2020-11": 300,每个组都有它的每月总价值,例如

代码语言:javascript
复制
groupMonthlyIncomes: {
 "2020-11": 60,
 "2020-12": 90
}

到目前为止我所做的是:

代码语言:javascript
复制
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
    }
  })
));

有人能让我知道下一步应该是什么吗?或者,如果有更好的方法,我可以找到其他的解决方案吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-01-20 12:30:19

试试这个:

代码语言:javascript
复制
  incomeRows.map((row) => {
  const reportValues = row.categories.reduce((result, next) => {
    const catKey = next.category;
    const total = row.categories.
      filter((_cat) => _cat.category === next.category && _cat.transdate === next.transdate)
      .reduce((accum, _next) => accum + _next.reportvalue, 0);
    result[catKey] = result[catKey] ? {...result[catKey], [next.transdate]: total} : {[next.transdate]: total};
    return result;
  }, {});
  return {
    group: row.group,
    categories: Object.keys(reportValues).map((_key) => {
      return {
        category: _key,
        ...reportValues[_key],
      }
    }),
    groupMonthlyIncomes: row.categories.reduce((result, next) => {
      const key = next.transdate;
      result[key] = result[key] ? result[key] + next.reportvalue : next.reportvalue;
      return result;
    }, {}),
  }
});
票数 1
EN

Stack Overflow用户

发布于 2022-01-20 00:24:21

试试这个:

代码语言:javascript
复制
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;
    }, {}),
  }
});
票数 1
EN

Stack Overflow用户

发布于 2022-01-20 05:32:12

代码语言:javascript
复制
const reorganization = () => {

    // declares an array of property names
    const attrs = ['category', 'transdate', 'reportvalue', 'categories', 'groupMonthlyIncomes'];

    return incomeRows.map(row => {

        // do something for categories
        const categorieMap = new Map([...new Map(row.categories.map(item => [item[attrs[0]], new Map()]))]);
        const categories = [...categorieMap.keys()];
        for (let i = 0; i < categories.length; i++) {
            const _map = categorieMap.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;

        // do something for groupMonthlyIncomes
        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));

        return row;
    });
}

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

console.log(reorganization());

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70779252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档