Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

array-unique-by.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var aCallable = require('../internals/a-callable');
  4. var isNullOrUndefined = require('../internals/is-null-or-undefined');
  5. var lengthOfArrayLike = require('../internals/length-of-array-like');
  6. var toObject = require('../internals/to-object');
  7. var MapHelpers = require('../internals/map-helpers');
  8. var iterate = require('../internals/map-iterate');
  9. var Map = MapHelpers.Map;
  10. var mapHas = MapHelpers.has;
  11. var mapSet = MapHelpers.set;
  12. var push = uncurryThis([].push);
  13. // `Array.prototype.uniqueBy` method
  14. // https://github.com/tc39/proposal-array-unique
  15. module.exports = function uniqueBy(resolver) {
  16. var that = toObject(this);
  17. var length = lengthOfArrayLike(that);
  18. var result = [];
  19. var map = new Map();
  20. var resolverFunction = !isNullOrUndefined(resolver) ? aCallable(resolver) : function (value) {
  21. return value;
  22. };
  23. var index, item, key;
  24. for (index = 0; index < length; index++) {
  25. item = that[index];
  26. key = resolverFunction(item);
  27. if (!mapHas(map, key)) mapSet(map, key, item);
  28. }
  29. iterate(map, function (value) {
  30. push(result, value);
  31. });
  32. return result;
  33. };