How to map and group array of objects

javascript group by multiple properties
javascript group array of objects by property
lodash group by
group array of objects by key c#
group array of objects by key angular
javascript array group by sum
javascript array group by count
group array of objects by key php

I'm building an application with Node.js, Express, Postgres and Sequelize.

I get a response that looks like this:

[
    {
        "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"
            }
        ]
    }
]

I want to group all the events that happen on the same date.

I tried

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

but it doesn't work.

How can I map and group an array?

Eventually I want to have an object (or array) that looks something like this:

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

You can use 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)

How to group an array of objects through a key using Array reduce , Steps to create the groupBy function, create an object as initial value for our result object. inside the array reduce, create an array for each distinct key value and push the currentValue if the key value present in the currentValue. For the person array, lets group the objects using the color value. In our object, there are two blue color object and one green color object. // Accepts the array and key const groupBy = ( array , key ) => { // Return the end result return array . reduce (( result , currentValue ) => { // If an array already present for key, push it to the array.

Try this:

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)

JavaScript group an array of objects by key, In this article, I will explain about the groupBy array of object in javascript.. “ JavaScript group an array of objects by key” is published by Edison� I now have to to group them by sales person. In my mind I saw it as loop/map through each campaign object in campaigns array, check the createdBy name and return a new array of objects that have the results group by createdBy name. Sounded easy in my head but can't get around it. I have put together the following:

You can use the function reduce for grouping the objects by date.

This is assuming the array has only one index.

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

javascript group by property array of objects Code Example, “javascript group by property array of objects” Code Answer based on another array of objects in javascript � groupby vs map javascript� Let us assume that you have an object declared multiple properties. First Get the named keys using object.keys() method. This method retrieves keys from the given object and returns an array of keys. Using map() method with a defined callback. And callback is executed for each element of an object.

You can also use this to accomplish what you're looking for.

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); 

})

group by typescript array Code Example, Get code examples like "group by typescript array" instantly right from your google search results with the Grepper Chrome const collection = map.get(key );. 6. Now we need to merge the two array of objects into a single array by using id property because id is the same in both array objects. Note: Both arrays should be the same length to get a correct answer. First way. Here we are the using map method and Object.assign method to merge the array of objects by using id.

Well i didn't see that many answers but since i did it too

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

const bookingsByDate = {};

data.forEach((booking) => {    

    booking.bookings.forEach((bookingDate) => {
        const date = new Date(bookingDate.date);
        const day = date.getDate() < 10 ?  '0' + date.getDate(): date.getDate();
        const month = date.getMonth() < 10 ? '0' + date.getMonth(): date.getMonth();
        const year = date.getFullYear();

        const fullDate = year + '-' + month + '-' + day;

        if(!bookingsByDate[fullDate]) {
            bookingsByDate[fullDate] = [];
        }
        bookingsByDate[fullDate] = [
            ...bookingsByDate[fullDate],
            { 
                id: booking.id,
                type: booking.type,
            }
        ]
    });

})
console.log(bookingsByDate);

Group Array of JavaScript Objects by Key or Property Value � GitHub, Group Array of JavaScript Objects by Key or Property Value. Implementation. const groupBy = key => array => array.reduce((objectsByKeyValue, obj) => { const� A common case of using this is converting an array of objects into a hash map of objects. Code. The following is a small snippet in JavaScript to convert an array of objects to a hash map, indexed by the attribute value of object. You can provide a function to evaluate the key of hash map dynamically (run time). Hope this helps someone in future.

Most efficient method to groupby on an array of objects in Javascript, The most efficient method to group by a key on an array of objects in js is to use the reduce function.The reduce() method executes a reducer� Just like .map(), .reduce() also runs a callback for each element of an array. What’s different here is that reduce passes the result of this callback (the accumulator) from one array element to

jQuery.map(), map() method applies a function to each item in an array or object and maps the results into a new array. Prior to jQuery 1.6, $.map() supports traversing arrays� The callback in .map shouldn't return array-- it should return the new value you want that particular item in the array to have. docs = docs.map(function(item){ item.description = item.description.slice(0, 10); return item; }); If all you're doing is transforming each item in the array, it would be more performant to use .forEach instead. .map

Grouping elements in array by multiple properties, function groupBy( array , f ) { var groups = {}; array. return Object.keys(groups). map( function( group ) { return groups[group]; }) } var result�

Comments
  • Can those bookings arrays have more than a single date-containing object?
  • At that point no. Dates can be confirmed and proposed. In the query I return only confirmed dates - always only one object.