Gathering detailed insights and metrics for sort-object
Gathering detailed insights and metrics for sort-object
Gathering detailed insights and metrics for sort-object
Gathering detailed insights and metrics for sort-object
npm install sort-object
Typescript
Module System
Min. Node Version
Node Version
NPM Version
98.1
Supply Chain
97.5
Quality
76.2
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
75,726,072
Last Day
121,008
Last Week
752,064
Last Month
3,367,239
Last Year
30,808,162
MIT License
37 Stars
73 Commits
6 Forks
3 Watchers
2 Branches
4 Contributors
Updated on Jun 15, 2022
Latest Version
3.0.3
Package Id
sort-object@3.0.3
Size
2.91 kB
NPM Version
6.9.0
Node Version
10.15.2
Published on
Sep 19, 2019
Cumulative downloads
Total Downloads
Last Day
-3.5%
121,008
Compared to previous day
Last Week
-5.6%
752,064
Compared to previous week
Last Month
2.4%
3,367,239
Compared to previous month
Last Year
72.3%
30,808,162
Compared to previous year
6
Sort the keys in an object.
Install with npm:
1$ npm i sort-object --save
1var sortObj = require('sort-object');
By default, the keys on an object will be sorted in ascending order:
1sortObj({a: 1, c: 2, b: 3}); 2//=> {a: 1, b: 3, c: 2}
The second param can be an object of options
OR an array of keys
:
object
1sortObj({a: 1, c: 2, b: 3}, {keys: ['a', 'b']}); 2//=> {a: 1, b: 3}
array
1sortObj({a: 1, c: 2, b: 3}, ['a', 'c']); 2//=> {a: 1, c: 2}
keys
{Array} The returned object will contain only the specified keys, in the same order.sort
{Function} Sort function to sort the keys using JavaScript's .sort()
method.sortOrder
{String} Valid values are desc
or asc
, case insensitive.sortBy
{String} Sort function that is passed the entire object, rather than just the keys - as with the .sort()
method.Create a new object with only the given keys.
1var o = {a: 1, c: 2, e: 5, d: 4, b: 3}; 2sortObj(o, {keys: ['a', 'b']}); 3 4//=> {a: 1, b: 3}
Function to be passed to javascript's .sort()
method:
1var o = {a: 1, c: 2, e: 5, d: 4, b: 3}; 2var obj = sortObj(o, { 3 sort: function (a, b) { 4 return a < b ? -1 : 1; 5 } 6}); 7obj; 8//=> {a: 1, b: 3, c: 2, d: 4, e: 5}
Valid values are desc
or asc
, case insensitive:
1var o = {a: 1, c: 2, e: 5, d: 4, b: 3}; 2sortObj(o, {sortOrder: 'ASC'}); 3//=> {e: 5, d: 4, c: 3, b: 2, a: 1}
Function that returns an array of keys to sort by:
1var old = {one: 'aa', two: 'bc', three: 'ab'}; 2var o = sortObj(old, { 3 sortBy: function (obj) { 4 var arr = []; 5 Object.keys(obj).filter(function(key) { 6 if (/^a/.test(obj[key])) arr.push(key); 7 }); 8 return arr.reverse(); 9 } 10}); 11//=> {three: 'ab', one: 'aa'}
Brian Woodward
Copyright © 2014-2016 Brian Woodward Released under the MIT license.
This file was generated by verb on February 03, 2016.
No vulnerabilities found.