Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Flattens a deeply nested object into a object containing only keys and values. Previously nested objects have the hierarchy represented as a keypath in their property key.
object - A deeply nested Object to flatten.
delimiter - A delimiter to use in the keypaths (default: ".")
Returns the flattened object.
var data = {
track: {
name: 'Michelle',
url: 'http://www.last.fm/music/The+Beatles/Rubber+Soul/Michelle',
album: {
name: 'Rubber Soul'
url: 'http://www.last.fm/music/The+Beatles/Rubber+Soul'
tracks: 12
},
artist: {
name: 'The Beatles',
url: 'http://www.last.fm/music/The+Beatles'
}
}
};
flatten(data);
// => returns {
// 'track.name': 'Michelle',
// 'track.url': 'http://www.last.fm/music/The+Beatles/Rubber+Soul/Michelle',
// 'track.album.name': 'Rubber Soul'
// 'track.album.url': 'http://www.last.fm/music/The+Beatles/Rubber+Soul'
// 'track.album.tracks': 12
// 'track.artist.name': 'The Beatles',
// 'track.artist.url': 'http://www.last.fm/music/The+Beatles'
// }
To use with node.js:
var flatten = require('flatten').flatten;
flatten(data);
Expands a previously flattened object restoring keys with keypaths
into full objects. Also accepts an optional delimiter
to manually
specify a keypath.
object - A flattened Object to restore.
delimiter - A delimiter to use in the keypaths (default: ".")
Returns the newly expanded object.
var data = {
'track.name': 'Michelle',
'track.url': 'http://www.last.fm/music/The+Beatles/Rubber+Soul/Michelle',
'track.album.name': 'Rubber Soul'
'track.album.url': 'http://www.last.fm/music/The+Beatles/Rubber+Soul'
'track.album.tracks': 12
'track.artist.name': 'The Beatles',
'track.artist.url': 'http://www.last.fm/music/The+Beatles'
};
expand(data);
// => returns data object as seen in flatten example above.
To use with node.js:
var expand = require('flatten').expand;
expand(data);
Both functions have a .noConflict()
method that allows you to reassign
them to another variable in the same way as jQuery.noConflict()
.
To assign the functions to another library (eg. Underscore):
// Extend underscore.js
_.mixin({
flattenObject: flatten.noConflict(),
expandObject: expand.noConflict()
});
// Extend jQuery
$.extend({
flatten: flatten.noConflict(),
expand: expand.noConflict()
});
Released under the MIT license
FAQs
Inflate and deflate nested objects
We found that flatten.js demonstrated a not healthy version release cadence and project activity because the last version was released 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
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.