Completely Removing Duplicate Items from an Array

Remove duplicate values from JS array

Quick and dirty using jQuery:

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});

Completely removing duplicate items from an array

You could use Array#filter with Array#indexOf and Array#lastIndexOf and return only the values which share the same index.

var array = [1, 2, 3, 4, 4, 5, 5],    result = array.filter(function (v, _, a) {        return a.indexOf(v) === a.lastIndexOf(v);    });
console.log(result);

How to remove all duplicates from an array of objects?

A primitive method would be:

const obj = {};

for (let i = 0, len = things.thing.length; i < len; i++) {
obj[things.thing[i]['place']] = things.thing[i];
}

things.thing = new Array();

for (const key in obj) {
things.thing.push(obj[key]);
}

Remove duplicate values from an array of objects in javascript

You can use array#reduce and array#some.

const arr = [    {label: 'All', value: 'All'},    {label: 'All', value: 'All'},    {label: 'Alex', value: 'Ninja'},    {label: 'Bill', value: 'Op'},    {label: 'Cill', value: 'iopop'}]
var result = arr.reduce((unique, o) => { if(!unique.some(obj => obj.label === o.label && obj.value === o.value)) { unique.push(o); } return unique;},[]);console.log(result);

Removing duplicates from an array leaves duplicate values in the end result

Solution:

function removeDuplicate(arr) {
var i = 0;
var j = 1;
while (j < arr.length) {
if (arr[i] === arr[j]) {
j++;
} else {
arr[++i] = arr[j];
j++;
}
}
for(let k = arr.length; k > i+1; k--){
arr.pop()
}

return arr;
}

const ans = removeDuplicate([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])
console.log(ans);

Remove duplicate items from the array retracted from api

To keep the id of the last occurence you can create a Map of the array keyed by name and then convert back to an array using the iterator returned by Map.values(). This works by overwriting earlier entries in the Map with the same name.

const users = [{ "id": 2, "name": "user_1" }, { "id": 3, "name": "user_3" }, { "id": 4, "name": "user_3" }];

const result = [...new Map(users.map((user) => [user.name, user])).values()];

console.log(result);
// [ { id: 2, name: 'user_1' }, { id: 4, name: 'user_3' } ]

Removing duplicate objects (based on multiple keys) from array

You could use a Set in a closure for filtering.

const    listOfTags = [{ id: 1, label: "Hello", color: "red", sorting: 0 }, { id: 2, label: "World", color: "green", sorting: 1 }, { id: 3, label: "Hello", color: "blue", sorting: 4 }, { id: 4, label: "Sunshine", color: "yellow", sorting: 5 }, { id: 5, label: "Hello", color: "red", sorting: 6 }],    keys = ['label', 'color'],    filtered = listOfTags.filter(        (s => o =>             (k => !s.has(k) && s.add(k))            (keys.map(k => o[k]).join('|'))        )        (new Set)    );
console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Related Topics



Leave a reply



Submit