https://github.com/writetome51/get-grouped-by-properties
Separates array of objects into sub-arrays of objects with matching property values
https://github.com/writetome51/get-grouped-by-properties
array grouping javascript object-sort objects sorting typescript
Last synced: about 2 months ago
JSON representation
Separates array of objects into sub-arrays of objects with matching property values
- Host: GitHub
- URL: https://github.com/writetome51/get-grouped-by-properties
- Owner: writetome51
- License: mit
- Created: 2019-02-12T21:20:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-17T22:21:12.000Z (over 4 years ago)
- Last Synced: 2025-05-23T06:27:23.624Z (about 1 year ago)
- Topics: array, grouping, javascript, object-sort, objects, sorting, typescript
- Language: TypeScript
- Homepage:
- Size: 44.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getGroupedByProperties\(
properties: string[],
objects: T[],
matchFunctions?: {
[property: string]: (a, b) => boolean
}
): Array
Returns `objects` separated into groups (sub-arrays). Each group will contain objects with
matching values of every property in `properties`. You can customize how a match is determined
with the optional `matchFunctions`.
`matchFunctions`: object. Any of its keys must be identical to a property in `properties`.
The value of each key must be this type of function: `(a, b) => boolean` . It's called to
determine a match when grouping by the property matching that particular key. If a matching key
isn't provided for a particular property, the default matchFunction
`(a, b) => String(a) === String(b)` will be used for that property.
You can change the default matchFunction with the key `'$default'`.
The `properties` can each contain dot-notation, i.e, `'property.subproperty.subsubproperty'`.
Even if a property is an array index, here you need to use dot-notation and not
square braces, i.e. `'1.0' // instead of [1][0]`
The order you list the `properties` determines the order the groups are returned in.
## Examples
```js
let persons = [
{name: {first: 'Danny', last: 'Jones'}, address:'800 N. First St.'},
{name: {first: 'Michael', last: 'Watts'}, address:'100 S. Palm Way'},
{name: {first: 'Robert', last: 'Walters'}, address:'200 W. Elm St.'},
{name: {first: 'Sara', last: 'Watts'}, address:'100 S. Palm Way'},
{name: {first: 'Carol', last: 'Jones'}, address:'800 N. First St.'},
{name: {first: 'Tara', last: 'Zucko'}, address:'100 S. Palm Way'},
];
getGroupedByProperties(['address', 'name.last'], persons);
/******************
Returns:
[
[
{name: {first: 'Michael', last: 'Watts'}, address: '100 S. Palm Way'},
{name: {first: 'Sara', last: 'Watts'}, address: '100 S. Palm Way'}
],
[ {name: {first: 'Tara', last: 'Zucko'}, address:'100 S. Palm Way'} ],
[ {name: {first: 'Robert', last: 'Walters'}, address: '200 W. Elm St.'} ],
[
{name: {first: 'Danny', last: 'Jones'}, address: '800 N. First St.'},
{name: {first: 'Carol', last: 'Jones'}, address: '800 N. First St.'}
]
]
******************/
// Reverse the order of properties to see the result:
getGroupedByProperties(['name.last', 'address'], persons);
/******************
Returns:
[
[
{name: {first: 'Danny', last: 'Jones'}, address: '800 N. First St.'},
{name: {first: 'Carol', last: 'Jones'}, address: '800 N. First St.'}
],
[ {name: {first: 'Robert', last: 'Walters'}, address: '200 W. Elm St.'} ],
[
{name: {first: 'Michael', last: 'Watts'}, address: '100 S. Palm Way'},
{name: {first: 'Sara', last: 'Watts'}, address: '100 S. Palm Way'}
],
[ {name: {first: 'Tara', last: 'Zucko'}, address:'100 S. Palm Way'} ]
]
******************/
// This example makes matching case-insensitive by default:
persons = [
{ name: { first: 'Danny', last: 'Jones' }, email: 'd_jonesy500@yahoo.com' },
{ name: { first: 'michael', last: 'watts' }, email: 'watts_my_name@gmail.com'},
{ name: { first: 'Michael', last: 'Watts' }, email: 'watts_my_name@gmail.com' },
{ name: { first: 'danny', last: 'jones' }, email: 'd_jonesy500@yahoo.com' }
];
getGroupedByProperties(
['name.last', 'email'],
persons,
{'$default': (a, b) => a.toLowerCase() === b.toLowerCase()}
);
/***************
Returns:
[
[
{ name: { first: 'Danny', last: 'Jones' }, email: 'd_jonesy500@yahoo.com' },
{ name: { first: 'danny', last: 'jones' }, email: 'd_jonesy500@yahoo.com' }
],
[
{ name: { first: 'Michael', last: 'Watts' }, email: 'watts_my_name@gmail.com' },
{ name: { first: 'michael', last: 'watts' }, email: 'watts_my_name@gmail.com' }
]
]
****************/
// This makes string matching case-insensitive and separates those younger than 100
// from those 100 and older:
persons = [
{name: {first: 'Eddie'}, age: 102},
{name: {first: 'Eddie'}, age: 32},
{name: {first: 'danny'}, age: 102},
{name: {first: 'eric'}, age: 25},
{name: {first: 'Danny'}, age: 31},
{name: {first: 'David'}, age: 100},
];
getGroupedByProperties(
// group by first letter of first name, and age:
['name.first.0', 'age'],
persons,
{
'name.first.0': (a, b) => a.toLowerCase() === b.toLowerCase(),
// Separate ages between those younger than 100, and everyone else:
'age': (a, b) => a < 100 ? b < 100 : b >= 100
}
);
/***********
Returns:
[
[ {name: {first: 'Danny'}, age: 31} ],
[ {name: {first: 'David'}, age: 100}, {name: {first: 'danny'}, age: 102} ],
[ {name: {first: 'eric'}, age: 25}, {name: {first: 'Eddie'}, age: 32} ],
[ {name: {first: 'Eddie'}, age: 102} ]
]
***********/
```
## Installation
```bash
npm i @writetome51/get-grouped-by-properties
```
## Loading
```js
import {getGroupedByProperties} from '@writetome51/get-grouped-by-properties';
```