8000 Add uniqByExisting function to handle missing property paths by vedant713 · Pull Request #5972 · lodash/lodash · GitHub
[go: up one dir, main page]

Skip to content

Add uniqByExisting function to handle missing property paths #5972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions dist/lodash.js
6820
Original file line number Diff line number Diff line change
Expand Up @@ -8454,8 +8454,52 @@
function uniq(array) {
return (array && array.length) ? baseUniq(array) : [];
}

/**
* This method is like `_.uniqBy` except that it skips elements where the
* specified property path doesn't exist. When using a property path string
* as the iteratee, objects that don't have that path will be excluded from
* the result entirely.
*
* @static
* @memberOf _
* @since 4.17.22
* @category Array
* @param {Array} array The array to inspect.
* @param {Function|string} [iteratee=_.identity] The iteratee invoked per element
* or the property path to check for existence.
* @returns {Array} Returns the new duplicate free array containing only objects
* where the property path exists.
* @example
*
* // With flat and nested objects
* var objects = [
* { name: 'Foo' },
* { name: 'Bar' },
* { person: { name: 'Foo' } },
* { person: { name: 'Bar' } }
* ];
*
* // uniqByExisting only includes objects where property path exists
* _.uniqByExisting(objects, 'person.name');
* // => [{ person: { name: 'Foo' } }, { person: { name: 'Bar' } }]
*/
function uniqByExisting(array, iteratee) {
if (!(array && array.length)) {
return [];
}

var func = getIteratee(iteratee, 2);
var isPropertyPath = typeof iteratee === 'string';

var filteredArray = isPropertyPath
? arrayFilter(array, function(value) {
return func(value) !== undefined;
})
: array;

return baseUniq(filteredArray, func);
}
/**
* This method is like `_.uniq` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the criterion by which
* uniqueness is computed. The order of result values is determined by the
Expand All @@ -8481,7 +8525,7 @@
function uniqBy(array, iteratee) {
return (array && array.length) ? baseUniq(array, getIteratee(iteratee, 2)) : [];
}

/**
* This method is like `_.uniq` except that it accepts `comparator` which
* is invoked to compare elements of `array`. The order of result values is
Expand Down
0