| 1 |
# shallow-copy |
| 2 |
|
| 3 |
make a shallow copy of an object or array |
| 4 |
|
| 5 |
[](https://ci.testling.com/substack/shallow-copy) |
| 6 |
|
| 7 |
[](http://travis-ci.org/substack/shallow-copy) |
| 8 |
|
| 9 |
# example |
| 10 |
|
| 11 |
you can copy objects shallowly: |
| 12 |
|
| 13 |
``` js |
| 14 |
var copy = require('shallow-copy'); |
| 15 |
|
| 16 |
var obj = { a: 3, b: 4, c: [5,6] }; |
| 17 |
var dup = copy(obj); |
| 18 |
dup.b *= 111; |
| 19 |
dup.c.push(7); |
| 20 |
|
| 21 |
console.log('original: ', obj); |
| 22 |
console.log('copy: ', dup); |
| 23 |
``` |
| 24 |
|
| 25 |
and you can copy arrays shallowly: |
| 26 |
|
| 27 |
``` js |
| 28 |
var copy = require('shallow-copy'); |
| 29 |
|
| 30 |
var xs = [ 3, 4, 5, { f: 6, g: 7 } ]; |
| 31 |
var dup = copy(xs); |
| 32 |
dup.unshift(1, 2); |
| 33 |
dup[5].g += 100; |
| 34 |
|
| 35 |
console.log('original: ', xs); |
| 36 |
console.log('copy: ', dup); |
| 37 |
``` |
| 38 |
|
| 39 |
# methods |
| 40 |
|
| 41 |
``` js |
| 42 |
var copy = require('shallow-copy') |
| 43 |
``` |
| 44 |
|
| 45 |
## copy(obj) |
| 46 |
|
| 47 |
Return a copy of the enumerable properties of the object `obj` without making |
| 48 |
copies of nested objects inside of `obj`. |
| 49 |
|
| 50 |
If `obj` is an array, the result will be an array. |
| 51 |
If `obj` is an object, the result will be an object. |
| 52 |
If `obj` is not an object, its value is returned. |
| 53 |
|
| 54 |
# install |
| 55 |
|
| 56 |
With [npm](https://npmjs.org) do: |
| 57 |
|
| 58 |
``` |
| 59 |
npm install shallow-copy |
| 60 |
``` |
| 61 |
|
| 62 |
# license |
| 63 |
|
| 64 |
MIT |