diff --git a/README.md b/README.md index 86eef2d..7e9ca76 100644 --- a/README.md +++ b/README.md @@ -1799,6 +1799,39 @@ Produces a duplicate-free version of the array, using === to test object equalit **[⬆ back to top](#quick-links)** +### _.uniqWith + +similar to _.uniq except that it accepts comparator which is invoked to compare elements of array. The order of result values is determined by the order they occur in the array. + + ```js + // Lodash + const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; + const result = _.uniqWith(objects, _.isEqual); + console.log(result); + // output: [{ x: 1, y: 2 }, { x: 2, y: 1 }] + + // Native + const uniqWith = (arr, fn) => arr.filter((element, index) => arr.findIndex((step) => fn(element, step)) === index); + + const array = [1, 2, 2, 3, 4, 5, 2]; + const result = uniqWith(array, (a, b) => a === b); + console.log(result); + // output: [1, 2, 3, 4, 5] + + const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; + const result = uniqWith(objects, (a, b) => JSON.stringify(a) === JSON.stringify(b)); + console.log(result); + // output: [{ x: 1, y: 2 }, { x: 2, y: 1 }] + ``` + +#### Browser Support for `Array.prototype.filter()` and `Array.prototype.findIndex()` + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 45.0 ✔ | 12.0 ✔ | 25.0 ✔ | ✖ | 32.0 ✔ | 8.0 ✔ | + +**[⬆ back to top](#quick-links)** + ## Function ### _.after @@ -3083,47 +3116,6 @@ Uppercases the first letter of a given string **[⬆ back to top](#quick-links)** -## Reference - -* [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference) -* [Underscore.js](http://underscorejs.org) -* [Lodash.js](https://lodash.com/docs) - -**[⬆ back to top](#quick-links)** - -### _.uniqWith - -similar to _.uniq except that it accepts comparator which is invoked to compare elements of array. The order of result values is determined by the order they occur in the array. - - ```js - // Lodash - const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; - const result = _.uniqWith(objects, _.isEqual); - console.log(result); - // output: [{ x: 1, y: 2 }, { x: 2, y: 1 }] - - // Native - const uniqWith = (arr, fn) => arr.filter((element, index) => arr.findIndex((step) => fn(element, step)) === index); - - const array = [1, 2, 2, 3, 4, 5, 2]; - const result = uniqWith(array, (a, b) => a === b); - console.log(result); - // output: [1, 2, 3, 4, 5] - - const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; - const result = uniqWith(objects, (a, b) => JSON.stringify(a) === JSON.stringify(b)); - console.log(result); - // output: [{ x: 1, y: 2 }, { x: 2, y: 1 }] - ``` - -### Browser Support for `Array.prototype.filter()` and `Array.prototype.findIndex()` - -![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] -:-: | :-: | :-: | :-: | :-: | :-: | - 45.0 ✔ | 12.0 ✔ | 25.0 ✔ | ✖ | 32.0 ✔ | 8.0 ✔ | - -**[⬆ back to top](#quick-links)** - ## Util ### _.times @@ -3243,7 +3235,8 @@ Checks if n is between start and up to, but not including, end. If end is not sp **[⬆ back to top](#quick-links)** - ### _.random +### _.random + Produces a random number between the inclusive lower and upper bounds. If only one argument is provided a number between 0 and the given number is returned. If floating is true, or either lower or upper are floats, a floating-point number is returned instead of an integer. ```js @@ -3299,7 +3292,7 @@ Produces a random number between the inclusive lower and upper bounds. If only o ``` - #### Browser Support for `Math.random()` +#### Browser Support for `Math.random()` ![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | @@ -3307,7 +3300,13 @@ Produces a random number between the inclusive lower and upper bounds. If only o **[⬆ back to top](#quick-links)** +## Reference +* [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference) +* [Underscore.js](http://underscorejs.org) +* [Lodash.js](https://lodash.com/docs) + +**[⬆ back to top](#quick-links)** ## Inspired by: