https://github.com/allanchain/jau
Just Anothor Unique array of objects
https://github.com/allanchain/jau
Last synced: about 1 year ago
JSON representation
Just Anothor Unique array of objects
- Host: GitHub
- URL: https://github.com/allanchain/jau
- Owner: AllanChain
- Created: 2020-04-18T13:38:56.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-22T01:33:35.000Z (about 6 years ago)
- Last Synced: 2025-02-17T06:45:54.937Z (over 1 year ago)
- Language: JavaScript
- Size: 104 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jau
Just Anothor Unique array of objects.
## `jau.unique`
jau can get unique objects by key(s) or a function. The first "unique object" is returned.
```js
const jau = require('jau')
// no param, simple equal will be used
jau.unique([1, 2, 2, 3, 1, 2, 4])
// [1, 2, 3, 4]
jau.unique(['a', 'a', 'b', 'a', 'c', 'a', 'd']
// ['a', 'b', 'c', 'd']
// with property name
jau.unique([{a: 1}, {a: 1, b: 2}, {b: 2}], 'a')
// [{a: 1}, {b: 2}]
jau.unique([{a: 1}, {a: 1, b: 2}, {b: 2}], 'b')
// [{a: 1}, {a: 1, b: 2}]
// with properties
jau.unique([{a: 1, b: 2, c: 1}, {a: 1, b: 2}, {b: 2}], ['a', 'b'])
// [{a: 1, b: 2, c: 1}, {b: 2}]
// with function
jau.unique([{a: 1, b: 2, c: 1}, {a: 1, b: 2}, {b: 2}],
(a, b) => a.a === b.a || a.b === b.b)
// [{a: 1, b: 2, c: 1}]
```
## `jau.group`
Sometimes you may need to get the last "unique object", or perform some merge operation. `jau.group` gives you control over this.
Similarly:
```js
// group: no param
jau.group([1, 2, 2, 3, 1, 2, 4]),
// [[1, 1], [2, 2, 2], [3], [4]]
// group: with property name
jau.group([{a: 1}, {a: 1, b: 2}, {b: 2}], 'a'),
// [[{a: 1}, {a: 1, b: 2}], [{b: 2}]]
// group: with properties
jau.group([{a: 1, b: 2, c: 1}, {a: 1, b: 2}, {b: 2}], ['a', 'b']),
// [[{a: 1, b: 2, c: 1}, {a: 1, b: 2}], [{b: 2}]]
// group: with function
jau.group([{a: 1, b: 2, c: 1}, {a: 1, b: 2}, {b: 2}],
(a, b) => a.a === b.a || a.b === b.b)
// [[{a: 1, b: 2, c: 1}, {a: 1, b: 2}, {b: 2}]]
```