Angular/Javascript - 转换数组对象的问题

发布于 2025-01-11 14:52:31 字数 1437 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活泼老夫 2025-01-18 14:52:31

如果您愿意使用 lodash下划线,这是一个解决方案:

const response = [{
    area: "Z",
    val: "work",
    count: 12,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "Z",
    val: "not work",
    count: 41,
    data: "2022-04-01T00:00:00"
  },
  {
    area: "R",
    val: "work",
    count: 55,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "R",
    val: "not work",
    count: 62,
    data: "2022-04-01T00:00:00"
  }
];

const group = _.groupBy(response, 'area');

const result = _.map(_.keys(group), i => {
  return {
    area: i,
    count: _.map(group[i], j => {
      return {
        [j.data.substring(0, 7)]: {
          [j.val]: j.count
        }
      }
    })
  }
})

console.log('result', result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

这是一个简单的 JS 解决方案。

const response = [{
    area: "Z",
    val: "work",
    count: 12,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "Z",
    val: "not work",
    count: 41,
    data: "2022-04-01T00:00:00"
  },
  {
    area: "R",
    val: "work",
    count: 55,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "R",
    val: "not work",
    count: 62,
    data: "2022-04-01T00:00:00"
  }
];


const groupBy = (arr, key) => {
  const initialValue = {};
  return arr.reduce((acc, cval) => {
    const myAttribute = cval[key];
    acc[myAttribute] = [...(acc[myAttribute] || []), cval]
    return acc;
  }, initialValue);
};

const group = groupBy(response, 'area');

const result = Object.keys(group).map(i => {
  return {
    area: i,
    count: group[i].map(j => {
      return {
        [j.data.substring(0, 7)]: {
          [j.val]: j.count
        }
      }
    })
  }
})

console.log(result)

If you are open to use lodash or underscore, here is a solution:

const response = [{
    area: "Z",
    val: "work",
    count: 12,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "Z",
    val: "not work",
    count: 41,
    data: "2022-04-01T00:00:00"
  },
  {
    area: "R",
    val: "work",
    count: 55,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "R",
    val: "not work",
    count: 62,
    data: "2022-04-01T00:00:00"
  }
];

const group = _.groupBy(response, 'area');

const result = _.map(_.keys(group), i => {
  return {
    area: i,
    count: _.map(group[i], j => {
      return {
        [j.data.substring(0, 7)]: {
          [j.val]: j.count
        }
      }
    })
  }
})

console.log('result', result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

This is a plain JS solution.

const response = [{
    area: "Z",
    val: "work",
    count: 12,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "Z",
    val: "not work",
    count: 41,
    data: "2022-04-01T00:00:00"
  },
  {
    area: "R",
    val: "work",
    count: 55,
    data: "2022-03-01T00:00:00"
  },
  {
    area: "R",
    val: "not work",
    count: 62,
    data: "2022-04-01T00:00:00"
  }
];


const groupBy = (arr, key) => {
  const initialValue = {};
  return arr.reduce((acc, cval) => {
    const myAttribute = cval[key];
    acc[myAttribute] = [...(acc[myAttribute] || []), cval]
    return acc;
  }, initialValue);
};

const group = groupBy(response, 'area');

const result = Object.keys(group).map(i => {
  return {
    area: i,
    count: group[i].map(j => {
      return {
        [j.data.substring(0, 7)]: {
          [j.val]: j.count
        }
      }
    })
  }
})

console.log(result)

梅窗月明清似水 2025-01-18 14:52:31

因为您的 count 语法无效。我假设它是一个对象数组。
使用reduce可以轻松解决这些分组问题

const  response = [{ area: "Z", val: "work", count: 12, data: "2022-03-01T00:00:00" },{ area: "Z", val: "not work", count: 41, data: "2022-04-01T00:00:00" },{ area: "R", val: "work", count: 55, data: "2022-03-01T00:00:00" },{ area: "R", val: "not work", count: 62, data: "2022-04-01T00:00:00" }];

const res = Object.values(response.reduce((acc,{area,val,count,data}) => {
    if(!acc[area])acc[area]={area: area, count:[]}
  acc[area]['count'].push({[data.substring(0,7)] : {[val]:count}})
  return acc
},{}))

console.log(res)

since your count is not valid syntax. i assumed it is an array of objects.
These grouping problems are easily solvable using reduce

const  response = [{ area: "Z", val: "work", count: 12, data: "2022-03-01T00:00:00" },{ area: "Z", val: "not work", count: 41, data: "2022-04-01T00:00:00" },{ area: "R", val: "work", count: 55, data: "2022-03-01T00:00:00" },{ area: "R", val: "not work", count: 62, data: "2022-04-01T00:00:00" }];

const res = Object.values(response.reduce((acc,{area,val,count,data}) => {
    if(!acc[area])acc[area]={area: area, count:[]}
  acc[area]['count'].push({[data.substring(0,7)] : {[val]:count}})
  return acc
},{}))

console.log(res)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文