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.

130 lines
5.1 KiB

4 years ago
  1. # unset-value [![NPM version](https://img.shields.io/npm/v/unset-value.svg?style=flat)](https://www.npmjs.com/package/unset-value) [![NPM monthly downloads](https://img.shields.io/npm/dm/unset-value.svg?style=flat)](https://npmjs.org/package/unset-value) [![NPM total downloads](https://img.shields.io/npm/dt/unset-value.svg?style=flat)](https://npmjs.org/package/unset-value) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/unset-value.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/unset-value)
  2. > Delete nested properties from an object using dot notation.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save unset-value
  7. ```
  8. ## Usage
  9. ```js
  10. var unset = require('unset-value');
  11. var obj = {a: {b: {c: 'd', e: 'f'}}};
  12. unset(obj, 'a.b.c');
  13. console.log(obj);
  14. //=> {a: {b: {e: 'f'}}};
  15. ```
  16. ## Examples
  17. ### Updates the object when a property is deleted
  18. ```js
  19. var obj = {a: 'b'};
  20. unset(obj, 'a');
  21. console.log(obj);
  22. //=> {}
  23. ```
  24. ### Returns true when a property is deleted
  25. ```js
  26. unset({a: 'b'}, 'a') // true
  27. ```
  28. ### Returns `true` when a property does not exist
  29. This is consistent with `delete` behavior in that it does not
  30. throw when a property does not exist.
  31. ```js
  32. unset({a: {b: {c: 'd'}}}, 'd') // true
  33. ```
  34. ### delete nested values
  35. ```js
  36. var one = {a: {b: {c: 'd'}}};
  37. unset(one, 'a.b');
  38. console.log(one);
  39. //=> {a: {}}
  40. var two = {a: {b: {c: 'd'}}};
  41. unset(two, 'a.b.c');
  42. console.log(two);
  43. //=> {a: {b: {}}}
  44. var three = {a: {b: {c: 'd', e: 'f'}}};
  45. unset(three, 'a.b.c');
  46. console.log(three);
  47. //=> {a: {b: {e: 'f'}}}
  48. ```
  49. ### throws on invalid args
  50. ```js
  51. unset();
  52. // 'expected an object.'
  53. ```
  54. ## About
  55. ### Related projects
  56. * [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.")
  57. * [get-values](https://www.npmjs.com/package/get-values): Return an array of all values from the given object. | [homepage](https://github.com/jonschlinkert/get-values "Return an array of all values from the given object.")
  58. * [omit-value](https://www.npmjs.com/package/omit-value): Omit properties from an object or deeply nested property of an object using object path… [more](https://github.com/jonschlinkert/omit-value) | [homepage](https://github.com/jonschlinkert/omit-value "Omit properties from an object or deeply nested property of an object using object path notation.")
  59. * [put-value](https://www.npmjs.com/package/put-value): Update only existing values from an object, works with dot notation paths like `a.b.c` and… [more](https://github.com/tunnckocore/put-value#readme) | [homepage](https://github.com/tunnckocore/put-value#readme "Update only existing values from an object, works with dot notation paths like `a.b.c` and support deep nesting.")
  60. * [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.")
  61. * [union-value](https://www.npmjs.com/package/union-value): Set an array of unique values as the property of an object. Supports setting deeply… [more](https://github.com/jonschlinkert/union-value) | [homepage](https://github.com/jonschlinkert/union-value "Set an array of unique values as the property of an object. Supports setting deeply nested properties using using object-paths/dot notation.")
  62. * [upsert-value](https://www.npmjs.com/package/upsert-value): Update or set nested values and any intermediaries with dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/doowb/upsert-value "Update or set nested values and any intermediaries with dot notation (`'a.b.c'`) paths.")
  63. ### Contributing
  64. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  65. ### Contributors
  66. | **Commits** | **Contributor** |
  67. | --- | --- |
  68. | 6 | [jonschlinkert](https://github.com/jonschlinkert) |
  69. | 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
  70. ### Building docs
  71. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  72. To generate the readme, run the following command:
  73. ```sh
  74. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  75. ```
  76. ### Running tests
  77. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  78. ```sh
  79. $ npm install && npm test
  80. ```
  81. ### Author
  82. **Jon Schlinkert**
  83. * [github/jonschlinkert](https://github.com/jonschlinkert)
  84. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  85. ### License
  86. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  87. Released under the [MIT License](LICENSE).
  88. ***
  89. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 25, 2017._