Last active
March 2, 2019 18:35
-
-
Save paulrobertlloyd/84059cc82d660c34195aa38939f3c075 to your computer and use it in GitHub Desktop.
Merging two object arrays?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'post-types': [{ | |
type: 'article', | |
name: 'Article', | |
path: { | |
template: 'app/templates/article.njk', | |
post: '_posts/{{ published }}-{{ slug }}.md', | |
file: 'images/{{ published }}/{{ slug }}/{{ filename }}', | |
url: '{{ published }}/{{ slug }}' | |
} | |
}, { | |
type: 'note', | |
name: 'Note', | |
path: { | |
template: 'app/templates/note.njk', | |
post: '_notes/{{ published }}-{{ slug }}.md', | |
url: 'notes/{{ published }}/{{ slug }}' | |
} | |
}, { | |
type: 'photo', | |
name: 'Photo', | |
path: { | |
template: 'app/templates/photo.njk', | |
post: '_photos/{{ published }}-{{ slug }}.md', | |
file: 'images/{{ published }}/{{ slug }}/{{ filename }}', | |
url: 'photos/{{ published }}/{{ slug }}' | |
} | |
}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'post-types': [{ | |
type: 'article', | |
name: 'CUSTOM VALUE', | |
path: { | |
template: 'CUSTOM VALUE' | |
} | |
}, { | |
type: 'note', | |
path: { | |
template: 'CUSTOM VALUE', | |
post: 'CUSTOM VALUE', | |
url: 'CUSTOM VALUE' | |
} | |
}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'post-types': [{ | |
type: 'article', | |
name: 'CUSTOM VALUE', | |
path: { | |
template: 'CUSTOM VALUE', | |
post: '_posts/{{ published }}-{{ slug }}.md', | |
file: 'images/{{ published }}/{{ slug }}/{{ filename }}', | |
url: '{{ published }}/{{ slug }}' | |
} | |
}, { | |
type: 'note', | |
name: 'Note', | |
path: { | |
template: 'CUSTOM VALUE', | |
post: 'CUSTOM VALUE', | |
url: 'CUSTOM VALUE' | |
} | |
}, { | |
type: 'photo', | |
name: 'Photo', | |
path: { | |
template: 'app/templates/photo.njk', | |
post: '_photos/{{ published }}-{{ slug }}.md', | |
file: 'images/{{ published }}/{{ slug }}/{{ filename }}', | |
url: 'photos/{{ published }}/{{ slug }}' | |
} | |
}] |
// this assumes you can import the JSON
var source = require("defaultConfig.js");
var override = require("publicationConfig.js");
function merge(source, destination) {
Object.keys(source).forEach((key) => {
if (typeof(source[key]) === "object") {
if (typeof(destination[key]) === "undefined") {
destination[key] = {};
}
merge(source[key], destination[key]);
} else {
destination[key] = source[key];
}
});
}
source.forEach((item) => {
let overrideItem = override.filter(o => o.type === item.type);
if (overrideItem.length === 1) {
merge(overrideItem[0], item);
}
});
source now contains the merged object array
A friend recommended I use Lodash, and seeing as this is server-side code, that seemed like a good shout, with few if any downsides. So, with the help of Lodash, here’s the code I ended up with using unionBy:
combinedConfig = _.unionBy(publicationConfig, defaultConfig, 'type');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I merge two arrays of objects, with merging based upon the value of
type.article
? E.g. ifdefaultConfig
has an object withtype: article
, merge that with the equivelent values inpublicationConfig
(which takes precedence in any merge).