Package Exports
- object-property-assigner
- object-property-assigner/build/index.cjs.js
- object-property-assigner/build/index.esm.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-property-assigner) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Object Property Assigner
Companion package to object-property-extractor
A lightweight (no dependencies) tool to assign deeply nested values in JS Objects (or Arrays).
Why?
Consider the object
const data = {
user: {
name: { first: 'Jango', last: 'Fett' },
children: ['Boba', 'Clone 1', 'Clone 2', ...etc],
weapons: [
{ name: 'Blaster', description: 'For shooting stuff' },
{ name: 'Seismic charge', description: '...BWAAAAAANG' },
],
},
...otherProperties,
}
In Javascript, you assign inner object properties via dot notation:
data.user.name.last = "Hutt"
If you want to assign a property dynamically, you can do this:
const key = "user"
data[key] = { name: "Boba Fett" } // data.user = { name: "Boba Fett" }
However, you can't do this:
const key = "user.name"
data[key] = "Boba Fett"
This tool allows assignation of deep properties using a single "property path" string.
Installation
yarn add object-property-assigner
// OR
npm install object-property-assigner
Usage
Note: function returns a shallow copy of the input, the original object is unmodified
assign( dataObject, propertyString, newValue, { options } )
(See below for options
details)
import assign from "object-property-assigner"
// Using the data object above
data = assign(data, "user.name.first", "Boba") // data.user.name.first = "Boba"
data = assign(data, "user.weapons[1].description", "Pew Pew") // data.user.weapons[1].description = "Pew Pew"
Array handling
In addition to accessing array by index (above), if an array consists of objects, then it's possible to assign a single property for all object in the array.
For example:
data = assign(data, "user.weapons.name", "Laser Gun")
// sets *all* user.weapons.name to "Laser Gun"
Options
The (optional) options
object can contain any or all of the following parameters:
remove
-- iftrue
, the property will be deleted rather than assigned (in which case thenewValue
parameter is ignored)assign(data, "user.name.first", null, {remove:true}) // delete user.name.first
createNew
-- (default:true
). If a property doesn't exist, it will be created, so set this tofalse
if this behaviour is not desiredNote: for arrays, if an index is specified higher than the current array length, a new item will be created as the next array item, regardless of how much higher the index is. e.g.assign(data, "user.kind", "Mandalorian", {createNew: true}) // data.user.kind = "Mandalorian"
assign( {myArray: [1, 2, 3]}, "myArray[10]", "New Value") // --> {myArray: [1, 2, 3, "New Value"]}
noError
-- (default:false
). If a property doesn't exist andcreateNew == false
, then an error will be thrown. If you'd rather it just silently ignored the missing property, then set this parameter totrue
. Note that this is only for errors due to invalid property strings -- other errors might still be thrown.
Testing
A jest test suite is included in the repo. To run:
yarn test
See /test/test.ts
for the test cases.
Bug report / Feature requests
Please make an issue in the Github repo: https://github.com/CarlosNZ/object-property-assigner