基于属性的JSON数组合并

时间:2019-01-31 06:10:45

标签: javascript arrays object javascript-objects array-merge

我需要将两个单独的JSON数组合并为一个数组...

我尝试做object.assign,但这只是覆盖,我只需要将其与dayOfWeek对象匹配。在没有jQuery的情况下如何实现此目标的任何方向都是首选。

结果不一定需要具有所有相同的参数,但是如果可以,那么将是加/加。

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)"
    }
]

需要合并到

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)"
    }
]

1 个答案:

答案 0 :(得分:2)

  • 使用daysOfWeek.forEach()数组进行迭代。
  • 使用Object.assign()将一个数组的对象与具有某些公共属性值的第二个数组的对象合并。
  • 使用.find()搜索openingHours中需要与daysOfWeek数组中的特定对象合并的对象。

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