https://github.com/hash-bang/lodash-keyarrange
Lodash plugin to correctly sort the keys within an object
https://github.com/hash-bang/lodash-keyarrange
Last synced: 20 days ago
JSON representation
Lodash plugin to correctly sort the keys within an object
- Host: GitHub
- URL: https://github.com/hash-bang/lodash-keyarrange
- Owner: hash-bang
- License: mit
- Created: 2015-06-18T03:43:01.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2025-02-20T06:24:14.000Z (3 months ago)
- Last Synced: 2025-05-05T22:56:37.696Z (20 days ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
lodash-keyarrange
=================
Lodash plugin to correctly sort the keys within an object.Why
---In JavaScript objects keys are generally stored in the order in which they are created. [ES6 formalizes this behaviour](http://www.ecma-international.org/ecma-262/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys).
var a = {};
a.foo = 1;
a.bar = 2;
a.baz = 3;
// a = { foo: 1, bar: 2, baz: 3 }But sometimes we need to output the object with the keys correctly sorted.
This lodash mixin allows key rearrangement within an object - returning a new object.
You can also deep sort an object with `keyArrangeDeep()`.
Example usage
-------------var _ = require('lodash')
.mixin(require('lodash-keyarrange'));var a = {foo: 'fooValue', bar: 'barValue', baz: 'bazValue'};
console.log( _.keyArrange(a) );
// Outputs { bar: 'barValue', baz: 'bazValue', foo: 'fooValue' }You can also provide a custom sorter for the keys:
// Example of reverse sorting keys
var obj = {foo: 'fooValue', bar: 'barValue', baz: 'bazValue'};console.log( _.keyArrange(obj, function(a, b) {
if (a < b) {
return 1;
} else if (b < a) {
return -1;
} else {
return 0;
}
} ) );// Outputs { foo: 'fooValue', baz: 'bazValue', bar: 'barValue' }
keyArrangeDeep()
----------------
It is also possible to traverse an object and arrange all keys. Scalars and array types are left unaltered but all objects will be run via the keyArrange function.For Example:
var _ = require('lodash')
.mixin(require('lodash-keyarrange'));var out = _.keyArrangeDeep({
gamma: [
{
omegaValue: 123,
gammaFoo: 'gammaFooValue',
},
{
kappa: {
gammaKappaBaz: 'hello',
},
},
1337,
],
alpha: 123,
si: {
theta: 999,
eta: 'value!',
},
beta: 'hello world',
});console.log(JSON.stringify(out, null, "\t"));
Will output:
{
"alpha": 123,
"beta": "hello world",
"gamma": [
{
"gammaFoo": "gammaFooValue",
"omegaValue": 123
},
{
"kappa": {
"gammaKappaBaz": "hello"
}
},
1337
],
"si": {
"eta": "value!",
"theta": 999
}
}