1.1.0 • Published 7 years ago

removeduplicates v1.1.0

Weekly downloads
56
License
ISC
Repository
github
Last release
7 years ago

removeDuplicates

Function that removes duplicates from an array.

Install

npm install removeduplicates

Usage

var removeDuplicates = require('removeDuplicates');

Remove duplicate items

var tempArray = ["tom", "jack", "jake", "tom"];

var uniqueArray = removeDuplicates(tempArray);

uniqueArray will now equal the following

["tom", "jack", "jake"];

Remove duplicate objects

Passing in your array of objects as the first parameter, and the key you're checking against as the second parameter. The function will remove any duplicate values based on this key.

var tempArray = [
    {
        id: 1234,
        selected: true,
        otherVal: 'abc'
    },
    {
        id: 5678,
        selected: false,
        otherVal: 'abc'
    },
    {
        id: 1234,
        selected: true,
        otherVal: 'def'
    }
];

var uniqueArray = removeDuplicates(tempArray, 'id');

uniqueArray will now equal the following

[
    {
        id: 1234,
        selected: true,
        otherVal: 'abc'
    },
    {
        id: 5678,
        selected: false,
        otherVal: 'abc'
    }
];