Skip to content

Function that removes duplicate items and objects based on a key from an array of objects.

Notifications You must be signed in to change notification settings

tjcafferkey/removeduplicates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'
    }
];

About

Function that removes duplicate items and objects based on a key from an array of objects.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published