Remove 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);
});

Remove duplicate objects from an array with Keys

Apply the technique shown in this answer, which is:

function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

...but using findIndex with some criteria rather than just indexOf.

let people = [
{ name: "George", lastname: "GeorgeLast", age: 12 },
{ name: "George", lastname: "GeorgeLast", age: 13 },
{ name: "Bob", lastname: "GeorgeLast", age: 12 }
]

let result = people.filter(
(person, index) => index === people.findIndex(
other => person.name === other.name
&& person.lastname === other.lastname
));
console.log(result);

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);

How to remove duplicate values from an array in PHP

Use array_unique().

Example:

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)

Remove duplicate from array of objects based on value of properties in JavaScript

You can use Map to club values by name and in case there are two values with same name just use the one without type = "new"

let someArray = [{id: 3, name:"apple", type: "new"}, {id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]

function getUnique(arr){
let mapObj = new Map()

arr.forEach(v => {
let prevValue = mapObj.get(v.name)
if(!prevValue || prevValue.type === "new"){
mapObj.set(v.name, v)
}
})
return [...mapObj.values()]
}

console.log(getUnique(someArray))

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

how to remove first element of duplicate in an array of objects

This will give you an array with the last value for duplicated elements, in preserved order. If you want exactly the 2nd one you need an extra flag

array=[{id:34,value:45}, {id:23,value:35}, {id:34,value:28}]

const obj = array.reduce ( (acc,cur,index) => {
acc[cur.id] = {index:cur};
return acc;
},{});

const output = Object.values(obj).sort( (a,b) => a.index - b.index).map( ({index:val}) => val )

console.log(output)

How do you remove duplicate values in array in Python?

You could simply use np.unique():

unique_values = np.unique(array)


Related Topics



Leave a reply



Submit