https://github.com/jaredreich/tread
👣 update an object with another object containing just the new key-value pairs. Objects can be infinitely nested, perfect for updating with PUT requests in your REST API
https://github.com/jaredreich/tread
deep javascript nested object objects recursive update
Last synced: 10 months ago
JSON representation
👣 update an object with another object containing just the new key-value pairs. Objects can be infinitely nested, perfect for updating with PUT requests in your REST API
- Host: GitHub
- URL: https://github.com/jaredreich/tread
- Owner: jaredreich
- License: mit
- Created: 2016-07-31T18:03:04.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-19T13:56:44.000Z (almost 9 years ago)
- Last Synced: 2025-03-25T03:13:19.265Z (10 months ago)
- Topics: deep, javascript, nested, object, objects, recursive, update
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Installation
``` bash
npm install --save tread
```
## Usage
```javascript
// user from your database
var user = {
details: {
name: {
first: 'Jane',
last: 'Smith'
},
address: {
street: {
number: '5',
name: 'Oak Lane'
},
coordinates: {
lat: 50,
lng: 50
}
}
},
gender: 'm'
}
// userUpdates from your PUT request
var userUpdates = {
details: {
name: {
first: 'John'
},
address: {
coordinates: {
lat: 100
}
}
}
}
// perform the update
user = tread(user, userUpdates, strict)
// save the updated user in the database
user.save(...)
```
#### Strict mode
strict (Boolean, default = false): only updates object if the key already existed.
For example:
```javascript
tread({ name: 'Jane' }, { name: 'John', age: 20 }, true)
// outputs
{ name: 'John' }
tread({ name: 'Jane' }, { name: 'John', age: 20 }, false)
// OR
tread({ name: 'Jane' }, { name: 'John', age: 20 })
// outputs
{ name: 'John', age: 20 }
```