Martin Mazza Dawson Martin Mazza Dawson - 1 year ago 89
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download