You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 regels
960 B

4 jaren geleden
  1. var baseSet = require('./_baseSet');
  2. /**
  3. * Sets the value at `path` of `object`. If a portion of `path` doesn't exist,
  4. * it's created. Arrays are created for missing index properties while objects
  5. * are created for all other missing properties. Use `_.setWith` to customize
  6. * `path` creation.
  7. *
  8. * **Note:** This method mutates `object`.
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 3.7.0
  13. * @category Object
  14. * @param {Object} object The object to modify.
  15. * @param {Array|string} path The path of the property to set.
  16. * @param {*} value The value to set.
  17. * @returns {Object} Returns `object`.
  18. * @example
  19. *
  20. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  21. *
  22. * _.set(object, 'a[0].b.c', 4);
  23. * console.log(object.a[0].b.c);
  24. * // => 4
  25. *
  26. * _.set(object, ['x', '0', 'y', 'z'], 5);
  27. * console.log(object.x[0].y.z);
  28. * // => 5
  29. */
  30. function set(object, path, value) {
  31. return object == null ? object : baseSet(object, path, value);
  32. }
  33. module.exports = set;