Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tpkn/add-props
Easiest way to add props and values to the object
https://github.com/tpkn/add-props
Last synced: 6 days ago
JSON representation
Easiest way to add props and values to the object
- Host: GitHub
- URL: https://github.com/tpkn/add-props
- Owner: tpkn
- License: mit
- Created: 2018-02-12T20:08:01.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T07:36:52.000Z (over 3 years ago)
- Last Synced: 2024-12-14T18:40:52.238Z (about 1 month ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Add Props [![npm Package](https://img.shields.io/npm/v/add-props.svg)](https://www.npmjs.org/package/add-props)
Easiest way to add props and values to the object## Installation
```bash
npm install add-props
```## Usage
```javascript
const addProps = require('add-props');var myObject = {};
// Just create properties structure
addProps(myObject, 'click.url');/*{
"click": {
"url": {}
}
}*/// Add value to the property
addProps(myObject, 'events.tracker.pixel.url', 12346789);/*{
"events": {
"tracker": {
"pixel": {
"url": 12346789
}
}
}
}*/// Create multiple properties and fill them with the same value
addProps(myObject, ['stat.user_id', 'html.footer.block_id'], 'id123456789');/*{
"stat": {
"user_id": "id123456789"
},
"html": {
"footer": {
"block_id": "id123456789"
}
}
}*/// Create multiple properties with own values
addProps(myObject, ['video.duration', 'audio.sound.muted'], [120.459, true]);/*{
"video": {
"duration": 120.459
},
"audio": {
"sound": {
"muted": true
}
}
}*/console.log(JSON.stringify(myObject, true, 3));
```Also you can create new object with specified props. Just skip the first argument like so:
```javascript
var new_object = addProps('new.object.says', 'yey!');
```