如何映射和分组对象数组

时间:2019-04-12 01:42:52

标签: javascript node.js lodash

我正在使用Node.js,Express,Postgres和Sequelize构建应用程序。

我收到如下响应:

[
    {
        "id": 101,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 102,
        "type": 4,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 103,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 104,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    }
]

我想对同一日期发生的所有事件进行分组。

我尝试了

_.forEach(response, function(value) {
    _.groupBy(value, value.bookings[0].date)
})

但它不起作用。

如何映射和分组数组?

最终我想要一个看起来像这样的对象(或数组):

{
    2019-04-15: [
        { id: 101, type: 0 }, { id: 103, type: 0}
    ],
    2019-04-17: [
        { id: 102, type: 4 }, { id: 104, type: 0}
    ]
}

5 个答案:

答案 0 :(得分:3)

您可以使用reduce

let data = [{"id": 101,"type": 0,"bookings": [{"date": "2019-04-15T02:00:00.000Z"}]},{"id": 102,"type": 4,"bookings": [{"date": "2019-04-17T02:00:00.000Z"}]},{"id": 103,"type": 0,"bookings": [{"date": "2019-04-15T02:00:00.000Z"}]},{"id": 104,"type": 0,"bookings": [{"date": "2019-04-17T02:00:00.000Z"}]}]

let op = data.reduce((op,{bookings,...rest}) => {
  let key = bookings[0].date.split('T',1)[0]
  op[key] = op[key] || []
  op[key].push(rest)
  return op
},{})

console.log(op)

答案 1 :(得分:0)

您可以使用函数reduce按日期对对象进行分组。

这是假设数组只有一个索引。

let arr = [    {        "id": 101,        "type": 0,        "bookings": [            {                "date": "2019-04-15T02:00:00.000Z"            }        ]    },    {        "id": 102,        "type": 4,        "bookings": [            {                "date": "2019-04-17T02:00:00.000Z"            }        ]    },    {        "id": 103,        "type": 0,        "bookings": [            {                "date": "2019-04-15T02:00:00.000Z"            }        ]    },    {        "id": 104,        "type": 0,        "bookings": [            {                "date": "2019-04-17T02:00:00.000Z"            }        ]    }];

let result = arr.reduce((a, {id, type, bookings: [{date}]}) => {
  let key = date.substring(0, 10);
  (a[key] || (a[key] = [])).push({id, type});
  return a;
}, Object.create(null));


console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

尝试一下:

const arr = [{
    "id": 101,
    "type": 0,
    "bookings": [{
        "date": "2019-04-15T02:00:00.000Z"
    }]
}, {
    "id": 102,
    "type": 4,
    "bookings": [{
        "date": "2019-04-17T02:00:00.000Z"
    }]
}, {
    "id": 103,
    "type": 0,
    "bookings": [{
        "date": "2019-04-15T02:00:00.000Z"
    },{
        "date": "2019-04-16T02:00:00.000Z"
    }]
}, {
    "id": 104,
    "type": 0,
    "bookings": [{
        "date": "2019-04-17T02:00:00.000Z"
    }]
}]

const yourObj = {};
arr.forEach(elem => {
    const bookings = elem.bookings;
    if (Array.isArray(elem.bookings)) {
        elem.bookings.forEach(obj => {
            const key = obj.date.split('T')[0];
//             var date = new Date(obj.date);
//             var key = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
               const {bookings, ...otherProp} = elem;
               yourObj[key] = yourObj[key] || [];
               yourObj[key].push(otherProp)
        })
    }
})

console.log(yourObj)

答案 3 :(得分:0)

您还可以使用它来完成所需的操作。

let response = [
    {
        "id": 101,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 102,
        "type": 4,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 103,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 104,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    }
]

let dateGroupings = {};

response.forEach((v)=> {
  let date = v.bookings[0].date.substring(0,10)
  if (!dateGroupings[date]){
    dateGroupings[date] = [];
  }

  let obj = { 
    id: v.id,
    type: v.type
  };

  dateGroupings[date].push(obj); 

})

答案 4 :(得分:0)

好吧,我没有看到太多答案,但是因为我也这样做了

abc.isx
def.isx
ghi.isx