根据一些计算将复杂的javascript数组简化为另一个数组?

时间:2018-10-21 07:40:06

标签: javascript ecmascript-6

我有一个对象数组

data: [
    {
        eventId: "EVEN00001",
        eventType: "call",
        startDate: "2018-08-01T21:30:17+05:30",
    },
    {
        eventId: "EVEN00002",
        eventType: "call",
        startDate: "2018-08-01T21:30:17+05:30",
    },
    {
        eventId: "EVEN00003",
        eventType: "meeting",
        startDate: "2017-02-03T21:30:17+05:30",
    },
    {
        eventId: "EVEN00004",
        eventType: "email",
        startDate: "2018-09-04T21:30:17+05:30",
    },
    {
        eventId: "EVEN00005",
        eventType: "meeting",
        startDate: "2018-09-05T21:30:17+05:30",
    }]

我想将其减少为

[{call: 2},{meeting: 2},{email: 1}]

如何使用javascript reduce方法实现此目标, 以某种方式,我能够达到预期的结果,但这不是正确的方法。

data.reduce((acc, item, i, arr) => {
        if (!acc[item.startDate]) {
            acc[item.startDate] = 1;
        } else {
            acc[item.startDate] += 1;
        }
        return acc;
    }, {});
const dayArray = Object.entries(trimedData)
    .reduce((acc, node, i, arr) => {

        let [days, val] = node;
        acc.push({
            days: days,
            value: val
        })
        return acc;
    }, []);

如何仅使用reduce。或其他更好的方法来达到预期的效果?

3 个答案:

答案 0 :(得分:3)

reduce放入由eventType索引的对象,该对象的值是该eventType的出现次数-每次迭代将适当的属性加1。然后,获取该对象的值:

const data=[{eventId:"EVEN00001",eventType:"call",startDate:"2018-08-01T21:30:17+05:30",},{eventId:"EVEN00002",eventType:"call",startDate:"2018-08-01T21:30:17+05:30",},{eventId:"EVEN00003",eventType:"meeting",startDate:"2017-02-03T21:30:17+05:30",},{eventId:"EVEN00004",eventType:"email",startDate:"2018-09-04T21:30:17+05:30",},{eventId:"EVEN00005",eventType:"meeting",startDate:"2018-09-05T21:30:17+05:30",}]

console.log(Object.values(
  data.reduce((a, { eventType }) => {
    if (!a[eventType]) a[eventType] = { [eventType] : 0 };
    a[eventType][eventType]++;
    return a;
  }, {})
));

答案 1 :(得分:2)

不确定为什么您的减少使用startDate而不是eventType

除此之外,您似乎处在正确的轨道上

let data = [{
    eventId: "EVEN00001",
    eventType: "call",
    startDate: "2018-08-01T21:30:17+05:30",
  },
  {
    eventId: "EVEN00002",
    eventType: "call",
    startDate: "2018-08-01T21:30:17+05:30",
  },
  {
    eventId: "EVEN00003",
    eventType: "meeting",
    startDate: "2017-02-03T21:30:17+05:30",
  },
  {
    eventId: "EVEN00004",
    eventType: "email",
    startDate: "2018-09-04T21:30:17+05:30",
  },
  {
    eventId: "EVEN00005",
    eventType: "meeting",
    startDate: "2018-09-05T21:30:17+05:30",
  }
];
let result = Object.entries(data.reduce((acc, {eventType}) => (acc[eventType] = (acc[eventType] || 0) + 1, acc), {})).map(([k, v]) => ({[k]:v}));

console.log(result);

答案 2 :(得分:2)

您可以轻松地将它们简化为一个对象,如果您真的想要数组,只需映射该对象的条目即可

const res = data.reduce((a, b) => {
  a[b.eventType] = (a[b.eventType] || 0) + 1;
  return a;
}, {});

const arr = Object.entries(res).map(([k, v]) => ({
  [k]: v
}));
console.log(arr);
<script>
  const data = [{
      eventId: "EVEN00001",
      eventType: "call",
      startDate: "2018-08-01T21:30:17+05:30",
    },
    {
      eventId: "EVEN00002",
      eventType: "call",
      startDate: "2018-08-01T21:30:17+05:30",
    },
    {
      eventId: "EVEN00003",
      eventType: "meeting",
      startDate: "2017-02-03T21:30:17+05:30",
    },
    {
      eventId: "EVEN00004",
      eventType: "email",
      startDate: "2018-09-04T21:30:17+05:30",
    },
    {
      eventId: "EVEN00005",
      eventType: "meeting",
      startDate: "2018-09-05T21:30:17+05:30",
    }
  ];
</script>