Skip to content

uniqueArray() Helper Method

The uniqueArray() Helper Method module provides a helper function that takes an array as an argument and returns a new array with any duplicate values in the argument array removed.

Import

To import the uniqueArray() Helper Method:

javascript
import { uniqueArray } from '@obewds/vueventus'

Arguments

Returns: String

ArgsTypeStatusDescription
arrayArrayRequiredAn array of values to reduce to only unique values and eliminate any duplicate values

Use Example

javascript
console.log(
    uniqueArray([true, 12, 'test string', true, 12, 'test string'])
)
// returns (array): [true, 12, 'test string']


console.log(
    uniqueArray([{prop:'test'}, [3,'a'], {prop:'test'}, [3,'a']])
)
// returns (array): [{prop:'test'}, [3,'a']]

Module Code

ts
// ./src/helpers/uniqueArray.ts

export default function(array: any[]): any[] {
    return array.filter((value, index, self) => self.indexOf(value) === index)
}

Released under the MIT License