What is object-path?
The object-path npm package provides utilities for accessing and manipulating deep properties of objects using a string path notation. It simplifies the process of getting, setting, and deleting nested properties in JavaScript objects.
What are object-path's main functionalities?
Get a nested property
This feature allows you to retrieve the value of a deeply nested property within an object using a string path.
const objectPath = require('object-path');
const obj = { a: { b: { c: 42 } } };
const value = objectPath.get(obj, 'a.b.c');
console.log(value); // 42
Set a nested property
This feature allows you to set the value of a deeply nested property within an object using a string path.
const objectPath = require('object-path');
const obj = { a: { b: { } } };
objectPath.set(obj, 'a.b.c', 42);
console.log(obj); // { a: { b: { c: 42 } } }
Delete a nested property
This feature allows you to delete a deeply nested property within an object using a string path.
const objectPath = require('object-path');
const obj = { a: { b: { c: 42 } } };
objectPath.del(obj, 'a.b.c');
console.log(obj); // { a: { b: { } } }
Check if a nested property exists
This feature allows you to check if a deeply nested property exists within an object using a string path.
const objectPath = require('object-path');
const obj = { a: { b: { c: 42 } } };
const exists = objectPath.has(obj, 'a.b.c');
console.log(exists); // true
Other packages similar to object-path
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes methods like _.get, _.set, and _.has for working with nested properties, similar to object-path. However, Lodash offers a broader set of utilities beyond just object manipulation.
dot-prop
Dot-prop is a lightweight package that provides similar functionality to object-path for getting, setting, and deleting nested properties using dot notation. It is simpler and more focused on object property manipulation compared to the more feature-rich Lodash.
deepdash
Deepdash is an extension of Lodash that adds deep traversal and manipulation capabilities. It provides methods for working with deeply nested properties, similar to object-path, but with additional features for deep operations on arrays and objects.
object-path
Access deep properties using a path
Install
Node.js
npm install object-path
Browser
bower install object-path
Usage
var obj = {
a: {
b: "d",
c: ["e", "f"]
}
};
var objectPath = require("object-path");
objectPath.get(obj, "a.b");
objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default');
objectPath.empty(obj, 'a.b');
objectPath.empty(obj, 'a.c');
objectPath.empty(obj, 'a');
objectPath.get(obj, "a.c.1");
objectPath.get(obj, ["a","c","1"]);
objectPath.get(obj, ["a.c.b"], "DEFAULT");
objectPath.set(obj, "a.h", "m");
objectPath.get(obj, "a.h");
objectPath.set(obj, "a.j.0.f", "m");
objectPath.insert(obj, "a.c", "m", 1);
objectPath.push(obj, "a.k", "o");
objectPath.ensureExists(obj, "a.k.1", "DEFAULT");
objectPath.del(obj, "a.b");
objectPath.del(obj, ["a","c",0]);
objectPath.has(obj, "a.b");
objectPath.has(obj, ["a","d"]);
var model = objectPath({
a: {
b: "d",
c: ["e", "f"]
}
});
model.get("a.b");
model.get(["a.c.b"], "DEFAULT");
model.del("a.b");
model.has("a.b");
Credits