Skip to content
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

Unique Array Helper #276

Open
MB-web opened this issue Jan 24, 2023 · 0 comments
Open

Unique Array Helper #276

MB-web opened this issue Jan 24, 2023 · 0 comments
Labels
js Needs changes in the area of js vue-3 Will only be done for the vue-3 version

Comments

@MB-web
Copy link
Collaborator

MB-web commented Jan 24, 2023

Consider adding a helper function for creating duplicate-free versions of arrays.
Should only be needed for object arrays since for arrays whose items are of primitive type new Set() can be used directly.

Input of Daniel Rotter was we could also consider adding a library like Underscore.js or Ramda.js for such js utility.

/**
 * Produces a duplicate-free version of an object array, using `new Set()` for test equality.
 * Not meant to be used for array which items are of primitive type.
 */
export default function<T>(input: T[]): T[] {
  if (!Array.isArray(input)) {
    return [];
  }

  return [...new Set(input.map(element => JSON.stringify(element)))].map(element => JSON.parse(element)) as T[];
}
@MB-web MB-web added vue-3 Will only be done for the vue-3 version js Needs changes in the area of js labels Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js Needs changes in the area of js vue-3 Will only be done for the vue-3 version
Projects
None yet
Development

No branches or pull requests

1 participant