Martin Mazza Dawson Martin Mazza Dawson - 3 months ago 6
Javascript Question

Lodash merge properties of object that are not in object already

I have an object:

var props = {//values here};

this.options: {
cssSelectorAncestor: props.cssSelectorAncestor,
media: {
autoPlay: props.autoPlay,
muted: props.muted
}
}

this.options = _.merge(
{
defaultPlaybackRate: 1.0,
minPlaybackRate: 0.5
}, props);


How do I merge all of the properties in props apart from the ones that have already been assigned to options.

Consider props immutable.

Answer

I'm just guessing here, because I'm having trouble following your sample code, but I think you want defaults():

const result = _.defaults({ foo: true }, { foo: false, bar: true });
// ➜ { foo: true, bar: true }

This will only merge in properties that don't already exist.