Socket
Socket
Sign inDemoInstall

group-array-then-sort

Package Overview
Dependencies
2
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    group-array-then-sort

env.json is a take on the traditional .env file pattern with an addition to be able to set the default from the same file opposed to additional logic.


Version published
Weekly downloads
3
decreased by-72.73%
Maintainers
1
Install size
63.8 kB
Created
Weekly downloads
 

Readme

Source

group-array-then-sort

npm i --save group-array-then-sort

What is it

Group and array of objects by a given key, then sort the groups of array by another key either asc or desc.

Example use:

Take this array of objects:

[{
  name: 'John',
  subject: 'English',
  score: 76,
  teacher: 'Mr Bob'
}, {
  name: 'John',
  subject: 'Science',
  score: 77,
  teacher: 'Mr Jim'
}, {
  name: 'John',
  subject: 'Maths',
  score: 56,
  teacher: 'Mr Ben'
}, {
  name: 'Janine',
  subject: 'Science',
  score: 62,
  teacher: 'Mr Jim'
}, {
  name: 'Janine',
  subject: 'Maths',
  score: 41,
  teacher: 'Mr Ben'
}]

You can then easily group the above by names into the following:

{ John: 
   [ { name: 'John', subject: 'Science', score: 77, teacher: 'Mr Jim' },
     { name: 'John', subject: 'Maths', score: 56, teacher: 'Mr Ben' },
     { name: 'John', subject: 'English', score: 76, teacher: 'Mr Bob' } 
   ],
  Janine: 
   [ { name: 'Janine', subject: 'Science', score: 62, teacher: 'Mr Jim' },
     { name: 'Janine', subject: 'Maths', score: 41, teacher: 'Mr Ben' } 
   ] 
 }

Using promises

const groupArrayThenSort = require('group-array-then-sort')
groupArrayThenSort(obj, 'name', 'subject', 'desc').then((sortedObj) => {
    console.log(sortedObj)
}).catch((e) => {
    console.error(e)
})

Using await

import groupArrayThenSort from 'group-array-then-sort'

// inside some async functino
let sortedObj
try{
   sortedObj = groupArrayThenSort(obj, 'name', 'subject', 'desc')
} catch (e) {
    console.error(e)
}    

See the tests in this repo for examples.

Keywords

FAQs

Last updated on 01 Nov 2017

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc