xxxxxxxxxx
function unique(a, fn) {
if (a.length === 0 || a.length === 1) {
return a;
}
if (!fn) {
return a;
}
for (let i = 0; i < a.length; i++) {
for (let j = i + 1; j < a.length; j++) {
if (fn(a[i], a[j])) {
a.splice(i, 1);
}
}
}
return a;
}
Code language: JavaScript (javascript)
xxxxxxxxxx
let arr = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];
const uniqueArray = arr.filter((v,i,a)=>a.findIndex(t=>(t.name===v.name))===i)
console.log(uniqueArray);
xxxxxxxxxx
// remove duplicates objects from an array of objects using javascript
let arr = [
{id: 1, name: 'Chetan'},
{id: 1, name: 'Chetan'},
{id: 2, name: 'Ujesh'},
{id: 2, name: 'Ujesh'},
];
let jsonArray = arr.map(JSON.stringify);
console.log(jsonArray);
/*
[
'{"id":1,"name":"Chetan"}',
'{"id":1,"name":"Chetan"}',
'{"id":2,"name":"Ujesh"}',
'{"id":2,"name":"Ujesh"}'
]
*/
let uniqueArray = [new Set(jsonArray)].map(JSON.parse);
console.log(uniqueArray);
/*
[ { id: 1, name: 'Chetan' }, { id: 2, name: 'Ujesh' } ]
*/
xxxxxxxxxx
const arr = [
{ id: 1, name: "test1" },
{ id: 2, name: "test2" },
{ id: 2, name: "test3" },
{ id: 3, name: "test4" },
{ id: 4, name: "test5" },
{ id: 5, name: "test6" },
{ id: 5, name: "test7" },
{ id: 6, name: "test8" }
];
const filteredArr = arr.reduce((acc, current) => {
const x = acc.find(item => item.id === current.id);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
xxxxxxxxxx
const array = [{id: 1, name: "hello"}, {id: 2, name: "hii"}, {id: 1, name: "hey"} ];
const cleanArray = array.reduce((unique, o) => {
if(!unique.some(obj => obj.id === o.id)) {
unique.push(o);
}
return unique;
},[]);
xxxxxxxxxx
arr.reduce((acc, current) => {
const x = acc.find(item => item.id === current.id);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
xxxxxxxxxx
// Join Without Dupes.
const joinWithoutDupes = (A, B) => {
const a = new Set(A.map(x => x.item))
const b = new Set(B.map(x => x.item))
return [A.filter(x => !b.has(x.item)), B.filter(x => !a.has(x.item))]
}
// Proof.
const output = joinWithoutDupes([{item:"apple",description: "lorem"},{item:"peach",description: "impsum"}], [{item:"apple", description: "dolor"},{item:"grape", description: "enum"}])
console.log(output)
xxxxxxxxxx
function unique(a, fn) {
if (a.length === 0 || a.length === 1) {
return a;
}
if (!fn) {
return a;
}
for (let i = 0; i < a.length; i++) {
for (let j = i + 1; j < a.length; j++) {
if (fn(a[i], a[j])) {
a.splice(i, 1);
}
}
}
return a;
}
const members = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 1, name: 'John' },
{ id: 4, name: 'Joe' },
];
const uniqueMembers = unique(
members,
(a, b) => (a.id === b.id) & (a.name === b.name)
);
console.log(uniqueMembers);
Code language: JavaScript (javascript)
xxxxxxxxxx
obj.arr = obj.arr.filter((value, index, self) =>
index === self.findIndex((t) => (
t.place === value.place && t.name === value.name
))
)
xxxxxxxxxx
function unique(a, fn) {
if (a.length === 0 || a.length === 1) {
return a;
}
if (!fn) {
return a;
}
for (let i = 0; i < a.length; i++) {
for (let j = i + 1; j < a.length; j++) {
if (fn(a[i], a[j])) {
a.splice(i, 1);
}
}
}
return a;
}
Code language: JavaScript (javascript)