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 }}' | |
} | |
}] |
This comment has been minimized.
This comment has been minimized.
// this assumes you can import the JSON
source now contains the merged object array |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
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).