This utility provides methods to evaluate and manipulate deeply nested properties of objects using string-based paths like:
"prop1[0].prop2[9]['name']"
You can use it to get or set values dynamically within complex object structures.
npm i property-accessor
Retrieves a value from the object using a dot/bracket notation path.
- Parameters:
key
(string
): Property path (e.g."prop1.prop2[0]['name']"
)target
(object
): The target object to evaluate
Sets a value at the given path. Returns true
on success, false
otherwise.
Automatically creates nested objects and arrays as needed.
- Parameters:
key
(string
): Property path (e.g."prop1.prop2[0]['name']"
)value
(any
): Value to settarget
(object
): The target object to modify
Returns flat object based on target.
- Parameters:
target
(object
): The target object
const { PropertyAccessor } = require('property-accessor');
const target = {
users: [
{ firstName: 'John', surName: 'Doe', age: 25 },
{ firstName: 'Ann', surName: 'Doe', age: 23 },
],
};
console.log(PropertyAccessor.get('users[0].firstName', target));
// Output: John
PropertyAccessor.set('users[1].age', 24, target); // true
console.log(PropertyAccessor.get('users[1].age', target));
// Output: 24
PropertyAccessor.set('users[0].children[0]', { name: 'Nick', age: 1 }, target);
console.log(target.users[0].children);
// Output: [ { name: 'Nick', age: 1 } ]
For backward compatibility, you can also create an instance and bind a specific target via the constructor:
const propertyAccessor = new PropertyAccessor({ a: [{ b: 3 }] });
propertyAccessor.set('a[1].b', 4);
propertyAccessor.get('a[1].b');
propertyAccessor.flat();