| 57 | * @returns {*|void} |
| 58 | */ |
| 59 | export function mergeObj(objA, objB, concatArr) { |
| 60 | function isObj(obj) { |
| 61 | return Object.prototype.toString.call(obj) === "[object Object]"; |
| 62 | } |
| 63 | function isArr(arr) { |
| 64 | return Object.prototype.toString.call(arr) === "[object Array]"; |
| 65 | } |
| 66 | if (!isObj(objA) || !isObj(objB)) return objA; |
| 67 | function deepMerge(objA, objB) { |
| 68 | forIn(objB, function (key) { |
| 69 | const subItemA = objA[key]; |
| 70 | const subItemB = objB[key]; |
| 71 | if (typeof subItemA === "undefined") { |
| 72 | objA[key] = subItemB; |
| 73 | } else { |
| 74 | if (isObj(subItemA) && isObj(subItemB)) { |
| 75 | /* 进行深层合并 */ |
| 76 | objA[key] = deepMerge(subItemA, subItemB); |
| 77 | } else { |
| 78 | if (concatArr && isArr(subItemA) && isArr(subItemB)) { |
| 79 | objA[key] = subItemA.concat(subItemB); |
| 80 | } else { |
| 81 | objA[key] = subItemB; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | }); |
| 86 | return objA; |
| 87 | } |
| 88 | return deepMerge(objA, objB); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Deep merge of multiple objects, the merge rules are based on mergeObj, but the concatArr option does not exist |