What is dot-object?
The dot-object npm package is a utility for transforming and manipulating JavaScript objects using dot notation. It allows you to easily convert between nested objects and flat objects, set and get values using dot notation, and remove properties from objects.
What are dot-object's main functionalities?
Flattening Objects
This feature allows you to convert a nested object into a flat object using dot notation. The code sample demonstrates how to flatten a nested object.
const Dot = require('dot-object');
const dot = new Dot();
const nested = { a: { b: { c: 1 } } };
const flattened = dot.dot(nested);
console.log(flattened); // { 'a.b.c': 1 }
Unflattening Objects
This feature allows you to convert a flat object with dot notation keys back into a nested object. The code sample demonstrates how to unflatten a flat object.
const Dot = require('dot-object');
const dot = new Dot();
const flattened = { 'a.b.c': 1 };
const nested = dot.object(flattened);
console.log(nested); // { a: { b: { c: 1 } } }
Setting Values
This feature allows you to set a value in an object using dot notation. The code sample demonstrates how to set a nested value in an object.
const Dot = require('dot-object');
const dot = new Dot();
const obj = {};
dot.set('a.b.c', 1, obj);
console.log(obj); // { a: { b: { c: 1 } } }
Getting Values
This feature allows you to get a value from an object using dot notation. The code sample demonstrates how to retrieve a nested value from an object.
const Dot = require('dot-object');
const dot = new Dot();
const obj = { a: { b: { c: 1 } } };
const value = dot.pick('a.b.c', obj);
console.log(value); // 1
Removing Properties
This feature allows you to remove a property from an object using dot notation. The code sample demonstrates how to delete a nested property from an object.
const Dot = require('dot-object');
const dot = new Dot();
const obj = { a: { b: { c: 1 } } };
dot.del('a.b.c', obj);
console.log(obj); // { a: { b: {} } }
Other packages similar to dot-object
flat
The 'flat' package provides similar functionality for flattening and unflattening objects. It is simple to use and focuses on converting nested objects to flat objects and vice versa. Compared to dot-object, 'flat' is more specialized and may be easier to use for basic flattening and unflattening tasks.
lodash
Lodash is a utility library that provides a wide range of functions for manipulating objects, arrays, and other data structures. It includes methods for deep setting, getting, and removing properties using dot notation. While Lodash offers more comprehensive functionality beyond object manipulation, it can be more complex to use compared to dot-object.
object-path
The 'object-path' package provides utilities for accessing and manipulating deep properties of objects using dot notation. It offers similar features to dot-object, such as setting, getting, and deleting properties. 'object-path' is focused on deep property manipulation and is a good alternative to dot-object for these specific tasks.
Dot-Object
Dot-Object makes it possible to transform javascript objects using dot notation.
Move a property within one object to another location
var DJ = require('dot-object');
var dj = new DJ();
var obj = {
'first_name': 'John',
'last_name': 'Doe'
};
dj.move('first_name', 'contact.firstname', obj);
dj.move('last_name', 'contact.lastname', obj);
console.log(obj);
{
contact: {
firstname: 'John',
lastname: 'Doe'
}
}
Transform an object
var DJ = require('dot-object');
var dj = new DJ();
var row = {
'id': 2,
'contact.name.first': 'John',
'contact.name.last': 'Doe',
'contact.email': 'example@gmail.com',
'contact.info.about.me': 'classified'
};
dj.object(row);
console.log(row);
{
"id": 2,
"contact": {
"name": {
"first": "John",
"last": "Doe"
},
"email": "example@gmail.com",
"info": {
"about": {
"me": "classified"
}
}
}
}
To convert manually per string use:
var DJ = require('dot-object');
var dj = new DJ();
var obj = { val: 'test' };
dj.str('this.is.my.string', 'value', obj);
console.log(obj);
{
"val": "test",
"this": {
"is": {
"my": {
"string": "value"
}
}
}
}
Pick a value using dot notation:
var obj = {
some: {
nested: {
value: 'Hi there!'
}
}
};
var val = dj.pick('some.nested.key', obj);
console.log(val);
Hi there!
Using modifiers
You can use modifiers to translate values on the fly.
This example uses the underscore.string library.
var DJ = require('dot-object');
var dj = new DJ();
var _s = require('underscore.string');
var row = {
'nr': 200,
'doc.name': ' My Document '
};
var mods = {
"doc.name": [_s.trim, _s.underscored],
};
dj.object(row, mods);
console.log(row);
{
"nr": 200,
"doc": {
"name": "my_document"
}
}
Or using .str() directy:
var DJ = require('dot-object');
var _s = require('underscore.string');
var obj = { id: 100 };
var dj = new DJ();
dj.str('my.title', 'this is my title', obj, _s.slugify);
dj.str('my.title', ' this is my title ', obj, [_s.trim, _s.slugify]);
console.log(obj);
Result:
{
"id": 100,
"my": {
"title": "this-is-my-title"
}
}
Using a different seperator
If you do not like dot notation, you are free to specify a different seperator.
var DJ = require('dot-object');
var dj = new DJ('->');
var _s = require('underscore.string');
var row = {
'nr': 200,
'doc->name': ' My Document '
};
var mods = {
"doc->name": [_s.trim, _s.underscored],
};
dj.object(row, mods);
console.log(row);
{
"nr": 200,
"doc": {
"name": "my_document"
}
}
Transforming SQL results to JSON
SQL translation on the fly:
Copyright © 2013 Rob Halff, released under the MIT license