Removing "Duplicate Objects"

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

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

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

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 objects from an Array

This one will assign this.subMenuItems an array containing only the first instance of each item because indexOf returns the first index where the object is found.

this.subMenuItems = this.items.filter((item, index, self) => self.indexOf(item) === index);

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

Remove duplicate objects in an array based on id and date

Does this solution works for you?

var arr = [
{fileID: "1234", pubDate: "04/13/2018", id: "4979146", jobID: "9146", downloadURL: "", title: null},
{fileID: "1235", pubDate: "04/13/2020", id: "4979147", jobID: "9147", downloadURL: "", title: null},
{fileID: "1236", pubDate: "02/23/2021", id: "4979148", jobID: "9148", downloadURL: "", title: null},
{fileID: "1237", pubDate: "01/15/2021", id: "4979148", jobID: "9148", downloadURL: "", title: null},
{fileID: "1238", pubDate: "05/17/2019", id: "4979146", jobID: "9146", downloadURL: "", title: null}
];

function getUniqItems(arr) {
const uniqueHash = arr.reduce((acc,o)=>{
if (!acc[o.id]) {
acc[o.id] = [];
}
acc[o.id].push(o);
return acc;
}
, {});
return Object.values(uniqueHash).reduce((acc,list)=>{
list.sort((a,b)=>new Date(b.pubDate) - new Date(a.pubDate));
acc.push(list[0]);
return acc;
}
, []);
}

console.log(getUniqItems(arr));

Remove duplicate object in Java 8 and get the sum of String values in the object (first value need to convert in BigDecimal)

You can create a method that adds two DTO:

private static DTO add(DTO dto1, DTO dto2) {
BigDecimal bd1 = new BigDecimal(dto1.getValue());
BigDecimal bd2 = new BigDecimal(dto2.getValue());
return new DTO(dto1.getId(), dto1.getYear(), bd1.add(bd2).toString());
}

Then you can stream grouping by id and reduce using the previous method:

private static List<DTO> add(List<DTO> list) {
Map<Integer, Optional<DTO>> map = list.stream()
.collect(Collectors.groupingBy(DTO::getId,
Collectors.reducing((d1, d2) -> add(d1, d2))));

return map.values().stream()
.filter(Optional::isPresent)
.map(Optional::get).toList();
}

Test:

List<DTO> list = List.of(
new DTO(1, 2020, "5.5"),
new DTO(1, 2020, "-8.0"),
new DTO(2, 2020, "1.5"),
new DTO(3, 2020, "4.5"),
new DTO(3, 2020, "1.5"),
new DTO(3, 2020, "-9.5"),
new DTO(4, 2020, "-3.5"),
new DTO(4, 2020, "7.5"),
new DTO(4, 2020, "5.5"),
new DTO(4, 2020, "-7.5"));

List<DTO> listAdded = add(list);

listAdded.forEach(System.out::println);

Output:

DTO[1, 2020, -2.5]
DTO[2, 2020, 1.5]
DTO[3, 2020, -3.5]
DTO[4, 2020, 2.0]


Related Topics



Leave a reply



Submit