Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thlorenz/unique-concat
Concatenates two arrays, removing duplicates in the process and returns one array with unique values.
https://github.com/thlorenz/unique-concat
Last synced: 2 months ago
JSON representation
Concatenates two arrays, removing duplicates in the process and returns one array with unique values.
- Host: GitHub
- URL: https://github.com/thlorenz/unique-concat
- Owner: thlorenz
- License: mit
- Created: 2013-07-28T13:18:09.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-07-28T15:33:45.000Z (over 11 years ago)
- Last Synced: 2024-05-08T17:30:26.775Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 84 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unique-concat [![build status](https://secure.travis-ci.org/thlorenz/unique-concat.png)](http://travis-ci.org/thlorenz/unique-concat)
[![testling badge](https://ci.testling.com/thlorenz/unique-concat.png)](https://ci.testling.com/thlorenz/unique-concat)
Concatenates two arrays, removing duplicates in the process and returns one array with unique values.
```js
var concat = require('unique-concat');
var res = concat([ 1, 2, 3 ], [ 1, 2, 3, 4, 5, 6])
console.log(res);
// => [1, 2, 3, 4, 5, 6]
```## Installation
npm install unique-concat
## API
###*function uniqueConcat(arr1, arr2[, identity])*
```
/**
* Concatenates two arrays, removing duplicates in the process and returns one array with unique values.
* In case the elements in the array don't have a proper built in way to determine their identity,
* a custom identity function must be provided.
*
* As an example, {Object}s all return '[ 'object' ]' when .toString()ed and therefore require a custom
* identity function.
*
* @name exports
* @function unique-concat
* @param arr1 {Array} first batch of elements
* @param arr2 {Array} second batch of elements
* @param identity {Function} (optional) supply an alternative way to get an element's identity
*/
```## Identity function example
```js
var identity = function (obj) { return obj.a; }
var res = concat([{ a: 1 }, { a: 2, b: 1}], [{ a: 2, b: 2 }, { a: 3 }], identity);
console.log(res);
// => [ { a: 1 }, { a: 2, b: 2 }, { a: 3 } ]
```For more examples see [tests](https://github.com/thlorenz/unique-concat/blob/master/test/index.js)
## License
MIT