简体   繁体   中英

JSON array merge based on property

I need to merge two separate JSON arrays into a single one...

I have tried doing object.assign but this just overrides and i need it to a match on the dayOfWeek object only. any direction on how this could be achieved without jQuery would be preferred.

The results don't necessarily need to have all the same parameters but if the could then that will be a plus / bonus.

var daysOfWeek = [
    {
        "dayOfWeek": 0,
        "name": "Sunday"
    },
    {
        "dayOfWeek": 1,
        "name": "Monday"
    },
    {
        "dayOfWeek": 2,
        "name": "Tuesday"
    },
    {
        "dayOfWeek": 3,
        "name": "Wednesday"
    },
    {
        "dayOfWeek": 4,
        "name": "Thursday"
    },
    {
        "dayOfWeek": 5,
        "name": "Friday"
    },
    {
        "dayOfWeek": 6,
        "name": "Saturday"
    }
]
var openingHours = [
    {
        "branchId": "a16ab1f8-0cfd-42eb-bc3e-25a8697003e4",
        "dayOfWeek": 6,
        "hours": {
            "startTime": "08:00",
            "endTime": "20:00"
        },
        "updatedAt": "Tue Jan 29 2019 10:41:33 GMT+0800 (Singapore Standard Time)",
        "createdAt": "Sun Aug 19 2018 04:32:45 GMT+0800 (Singapore Standard Time)"
    },
    {
        "branchId": "62111250-b1db-42a2-9686-daecc0442628",
        "dayOfWeek": 1,
        "hours": {
            "startTime": "08:00",
            "endTime": "20:00"
        },
        "updatedAt": "Sat Nov 24 2018 09:46:42 GMT+0800 (Singapore Standard Time)",
        "createdAt": "Mon Jul 16 2018 17:39:56 GMT+0800 (Singapore Standard Time)"
    },
    {
        "branchId": "f71dd838-1a09-4d78-bbf5-a3845c59e8cf",
        "dayOfWeek": 3,
        "hours": {
            "startTime": "08:00",
            "endTime": "20:00"
        },
        "updatedAt": "Wed Oct 31 2018 13:29:56 GMT+0800 (Singapore Standard Time)",
        "createdAt": "Wed Jun 06 2018 14:09:14 GMT+0800 (Singapore Standard Time)"
    }
]

needs to merge to

var daysOfWeek = [
    {
        "dayOfWeek": 0,
        "name": "Sunday"
    },
    {
        "branchId": "a16ab1f8-0cfd-42eb-bc3e-25a8697003e4",
        "dayOfWeek": 1,
        "name": "Monday",
        "hours": {
            "startTime": "08:00",
            "endTime": "20:00"
        },
        "updatedAt": "Tue Jan 29 2019 10:41:33 GMT+0800 (Singapore Standard Time)",
        "createdAt": "Sun Aug 19 2018 04:32:45 GMT+0800 (Singapore Standard Time)"
    },
    {
        "dayOfWeek": 2,
        "name": "Tuesday"
    },
    {
        "branchId": "a16ab1f8-0cfd-42eb-bc3e-25a8697003e4",
        "dayOfWeek": 3,
        "name": "Wednesday",
        "hours": {
            "startTime": "08:00",
            "endTime": "20:00"
        },
        "updatedAt": "Tue Jan 29 2019 10:41:33 GMT+0800 (Singapore Standard Time)",
        "createdAt": "Sun Aug 19 2018 04:32:45 GMT+0800 (Singapore Standard Time)"
    },
    {
        "dayOfWeek": 4,
        "name": "Thursday"
    },
    {
        "dayOfWeek": 5,
        "name": "Friday"
    },
    {
        "branchId": "a16ab1f8-0cfd-42eb-bc3e-25a8697003e4",
        "dayOfWeek": 6,
        "name": "Saturday",
        "hours": {
            "startTime": "08:00",
            "endTime": "20:00"
        },
        "updatedAt": "Tue Jan 29 2019 10:41:33 GMT+0800 (Singapore Standard Time)",
        "createdAt": "Sun Aug 19 2018 04:32:45 GMT+0800 (Singapore Standard Time)"
    }
]
  • Iterate over daysOfWeek array using .forEach() .
  • Use Object.assign() to merge object of one array with the object of second array having some common value of property.
  • Use .find() to search the object in openingHours that needs to be merged with a particular object in daysOfWeek array.

 var daysOfWeek = [ {"dayOfWeek": 0, "name": "Sunday"}, {"dayOfWeek": 1, "name": "Monday"}, {"dayOfWeek": 2, "name": "Tuesday"}, {"dayOfWeek": 3, "name": "Wednesday"}, {"dayOfWeek": 4, "name": "Thursday"}, {"dayOfWeek": 5, "name": "Friday"}, {"dayOfWeek": 6, "name": "Saturday"} ] var openingHours = [ {"branchId": "a16ab1f8-0cfd-42eb-bc3e-25a8697003e4", "dayOfWeek": 6, "hours": {"startTime": "08:00", "endTime": "20:00"}, "updatedAt": "Tue Jan 29 2019 10:41:33 GMT+0800 (Singapore Standard Time)", "createdAt": "Sun Aug 19 2018 04:32:45 GMT+0800 (Singapore Standard Time)"}, {"branchId": "62111250-b1db-42a2-9686-daecc0442628", "dayOfWeek": 1, "hours": {"startTime": "08:00", "endTime": "20:00"}, "updatedAt": "Sat Nov 24 2018 09:46:42 GMT+0800 (Singapore Standard Time)", "createdAt": "Mon Jul 16 2018 17:39:56 GMT+0800 (Singapore Standard Time)"}, {"branchId": "f71dd838-1a09-4d78-bbf5-a3845c59e8cf", "dayOfWeek": 3, "hours": {"startTime": "08:00", "endTime": "20:00"}, "updatedAt": "Wed Oct 31 2018 13:29:56 GMT+0800 (Singapore Standard Time)", "createdAt": "Wed Jun 06 2018 14:09:14 GMT+0800 (Singapore Standard Time)"} ]; daysOfWeek.forEach( o1 => Object.assign( o1, openingHours.find(o2 => o2.dayOfWeek === o1.dayOfWeek) ) ); console.log(daysOfWeek); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM