
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
object-property-assigner
Advanced tools
A lightweight (no dependencies) tool to assign deeply nested properties in JS Objects (incl. Arrays)
Companion package to object-property-extractor
A lightweight (no dependencies) tool to assign deeply nested values in JS Objects (or Arrays).
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.
yarn add object-property-assigner
// OR
npm install object-property-assigner
assign( dataObject, propertyString, newValue, { options } )
(See below for options
details)
import assign from "object-property-assigner"
// Using the data object above
assign(data, "user.name.first", "Boba") // data.user.name.first = "Boba"
assign(data, "user.weapons[1].description", "Pew Pew") // data.user.weapons[1].description = "Pew Pew"
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:
assign(data, "user.weapons.name", "Laser Gun")
// sets *all* user.weapons.name to "Laser Gun"
The (optional) options
object can contain any or all of the following parameters:
remove
-- if true
, the property will be deleted rather than assigned (in which case the newValue
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 to false
if this behaviour is not desired
assign(data, "user.kind", "Mandalorian", {createNew: true}) // data.user.kind = "Mandalorian"
noError
-- (default: false
). If a property doesn't exist and createNew == false
, then an error will be thrown. If you'd rather it just silently ignored the missing property, then set this parameter to true
. Note that this is only for errors due to invalid property strings -- other errors might still be thrownA jest test suite is included in the repo. To run:
yarn test
See /test/test.ts
for the test cases.
Please make an issue in the Github repo: https://github.com/CarlosNZ/object-property-assigner
FAQs
A lightweight (no dependencies) tool to assign deeply nested properties in JS Objects (incl. Arrays)
The npm package object-property-assigner receives a total of 83,393 weekly downloads. As such, object-property-assigner popularity was classified as popular.
We found that object-property-assigner demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.