setWith source npm

_.setWith(object, path, value, [customizer])

This method is like _.set except that it accepts customizer which is invoked to produce the objects of path. If customizer returns undefined path creation is handled by the method instead. The customizer is invoked with three arguments: (nsValue, key, nsObject).

Arguments

  1. object (Object)

    The object to modify.

  2. path (Array|string)

    The path of the property to set.

  3. value (*)

    The value to set.

  4. [customizer] (Function)

    The function to customize assigned values.

Returns (Object)

Returns object.

Example

function customizer(nsValue) {
  if (!_.isObject(nsValue)) {
    return {};
  }
}

_.setWith({ '0': { 'length': 2 } }, '[0][1][2]', 3, customizer);
// => { '0': { '1': { '2': 3 }, 'length': 2 } }