xxxxxxxxxx
this.item = this.item.filter((el, i, a) => i === a.indexOf(el))
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
const addresses = [ ]; // Some array I got from async call
const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
.map(id => {
return addresses.find(a => a.id === id)
})
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
arr.reduce((acc, current) => {
const x = acc.find(item => item.id === current.id);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
xxxxxxxxxx
function getUnique(arr, comp) {
// store the comparison values in array
const unique = arr.map(e => e[comp])
// store the indexes of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the false indexes & return unique objects
.filter((e) => arr[e]).map(e => arr[e]);
return unique;
}
console.log(getUnique(arr,'id'));
xxxxxxxxxx
let arr = [
{value: 'L7-LO', name: 'L7-LO'},
{value: '%L7-LO', name: '%L7-LO'},
{value: 'L7-LO', name: 'L7-LO'},
{value: '%L7-LO', name: '%L7-LO'},
{value: 'L7-L3', name: 'L7-L3'},
{value: '%L7-L3', name: '%L7-L3'},
{value: 'LO-L3', name: 'LO-L3'},
{value: '%LO-L3', name: '%LO-L3'}
];
let obj = {};
const unique = () => {
let result = [];
arr.forEach((item, i) => {
obj[item['value']] = i;
});
for (let key in obj) {
let index = obj[key];
result.push(arr[index])
}
return result;
}
arr = unique(); // for example;
console.log(arr);
xxxxxxxxxx
var filterArray = courseArray.reduce((accumalator, current) => {
if(!accumalator.some(item => item.id === current.id && item.name === current.name)) {
accumalator.push(current);
}
return accumalator;
},[]);
console.log(filterArray)