Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

paulrobertlloyd commented Mar 1, 2019

How can I merge two arrays of objects, with merging based upon the value of type.article? E.g. if defaultConfig has an object with type: article, merge that with the equivelent values in publicationConfig (which takes precedence in any merge).

@adamauckland

This comment has been minimized.

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

This comment has been minimized.

Copy link

adamauckland commented Mar 1, 2019

@paulrobertlloyd

This comment has been minimized.

Copy link
Owner Author

paulrobertlloyd commented Mar 2, 2019

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
You can’t perform that action at this time.