Skip to content

Instantly share code, notes, and snippets.

@paulrobertlloyd
Last active March 2, 2019 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulrobertlloyd/84059cc82d660c34195aa38939f3c075 to your computer and use it in GitHub Desktop.
Save paulrobertlloyd/84059cc82d660c34195aa38939f3c075 to your computer and use it in GitHub Desktop.
Merging two object arrays?
'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 }}'
}
}]
'post-types': [{
type: 'article',
name: 'CUSTOM VALUE',
path: {
template: 'CUSTOM VALUE'
}
}, {
type: 'note',
path: {
template: 'CUSTOM VALUE',
post: 'CUSTOM VALUE',
url: 'CUSTOM VALUE'
}
}]
'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 }}'
}
}]
@paulrobertlloyd
Copy link
Author

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