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 }}'
}
}]
@adamauckland
Copy link

adamauckland commented Mar 1, 2019

// 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

@adamauckland
Copy link

@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