1.0.1 • Published 4 years ago
js-shortcuts v1.0.1
js-shortcuts
It is a shortcut package for performing different JavaScript operations.
Installing
$ npm install js-shortcuts
For sorting
const sort = require('js-shortcuts').sort;
For array with elements
const sort = require('js-shortcuts').sort;
let arr = [3, 2, 1, 4, 5];
let sorted_array = sort(arr);
For array with objects
const sort = require('js-shortcuts').sort;
let arr = [
{
id: 3,
age: 21,
name: 'Robert'
},
{
id: 1,
age: 25,
name: 'John'
},
{
id: 2,
age: 20,
name: 'Jeff'
}
];
Sorting by id
let sorted_array = sort(arr, {property: ['id']});
Sorting by name
let sorted_array = sort(arr, {property: ['name']});
Sorting array with property inside parent object
Example: Sorting array with 'name' property
const sort = require('js-shortcuts').sort;
let arr = [
{
id: 2,
email: 'robert@xxx.com',
profile: {
name: 'Robert',
age: 23
}
},
{
id: 1,
email: 'john@xxx.com',
profile: {
name: 'John',
age: 24
}
}
];
let sorted_array = sort(arr, {property: ['profile', 'name']});
Additional attributes
Additional attribute can be provided to sort function for Ascending Order and Descending Order
sort(arr, {
order: 'asce',
property: ['id']
});
'asce' or 'ASCE' for Ascending Order and 'desc' or 'DESC' for Descending Order.
The default order is always Ascending Order