UNPKG

@extra-array/intersection

Version:

Gives values present in both arrays.

64 lines (49 loc) 2.22 kB
Gives values present in both arrays. [:package:](https://www.npmjs.com/package/@extra-array/intersection) [:smiley_cat:](https://github.com/orgs/nodef/packages?repo_name=extra-array) [:running:](https://npm.runkit.com/@extra-array/intersection) [:vhs:](https://asciinema.org/a/332072) [:moon:](https://www.npmjs.com/package/@extra-array/intersection.min) [:scroll:](https://unpkg.com/@extra-array/intersection/) [:newspaper:](https://nodef.github.io/extra-array/) [:blue_book:](https://github.com/nodef/extra-array/wiki/) > Similar: [union], [intersection], [difference], [symmetricDifference].<br> > Similar: [isUnique], [isDisjoint], [intersection]. > This is part of package [extra-array]. [extra-array]: https://www.npmjs.com/package/extra-array <br> ```javascript array.intersection(x, y, [fc], [fm]); // x: an array // y: another array // fc: compare function (a, b) // fm: map function (v, i, x) ``` > :stopwatch: Compare function => O(n²). ```javascript const array = require("extra-array"); var x = [1, 2, 3, 4]; var y = [2, 3, 5]; array.intersection(x, y); // [ 2, 3 ] var y = [-2, -3, -5]; array.intersection(x, y, (a, b) => Math.abs(a) - Math.abs(b)); // [ 2, 3 ] array.intersection(x, y, null, v => Math.abs(v)); // [ 2, 3 ] ``` <br> <br> ## References - [Data.Set.intersection: Haskell](http://hackage.haskell.org/package/containers-0.6.2.1/docs/Data-Set.html#v:intersection) - [Set.intersection: Python](https://www.programiz.com/python-programming/methods/set/intersection) - [_.intersection: lodash](https://lodash.com/docs/4.17.15#intersection) - [_.intersectionBy: lodash](https://lodash.com/docs/4.17.15#intersectionBy) - [_.intersectionWith: lodash](https://lodash.com/docs/4.17.15#intersectionWith) - [Array.intersect: sugarjs](https://sugarjs.com/docs/#/Array/intersect) [union]: https://github.com/nodef/extra-array/wiki/union [intersection]: https://github.com/nodef/extra-array/wiki/intersection [difference]: https://github.com/nodef/extra-array/wiki/difference [isUnique]: https://github.com/nodef/extra-array/wiki/isUnique [isDisjoint]: https://github.com/nodef/extra-array/wiki/isDisjoint [symmetricDifference]: https://github.com/nodef/extra-array/wiki/symmetricDifference