Javascript: Remove Duplicates In Array Of Objects Using A Single Property
Solution 1:
It's because you are always adding the id to the tempIndex
array, so it always thinks the current one is a duplicate. Try:
functiongetDistValues(object) {
var distObject = [];
var tempIndex = [];
var length = object.length;
for (var i = 0; i < length; i++) {
if (tempIndex.indexOf(object[i].id) === -1 || i === 0) {
tempIndex.push(object[i].id);
distObject.push(object[i]);
}
}
return distObject;
}
Solution 2:
You could take a Set
for id
property and filter if the id
is not in the set.
var array = [{ id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_three', value: 'value_three', label: 'ABX' }, { id: 'id_two', value: 'value_two', label: 'ABY' }, { id: 'id_four', value: 'value_four', label: 'ABD' }],
unique = array.filter((ids =>({ id }) => !ids.has(id) && ids.add(id))(newSet));
console.log(unique);
.as-console-wrapper { max-height: 100%!important; top: 0; }
An approach for older JS versions
var array = [{ id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_three', value: 'value_three', label: 'ABX' }, { id: 'id_two', value: 'value_two', label: 'ABY' }, { id: 'id_four', value: 'value_four', label: 'ABD' }],
ids = {},
unique = [],
i;
for (i = 0; i < array.length; i++) {
if (ids[array[i].id]) continue;
unique.push(array[i]);
ids[array[i].id] = true;
}
console.log(unique);
.as-console-wrapper { max-height: 100%!important; top: 0; }
Solution 3:
You can do it by storing the id
into an object
and check if the id
already exists in the object
. If exists that means you've taken the object before. If not then push the object into a new array.
This approach takes O(n)
time as we check from an object with constant time.
var arr = [
{id: 'id_one', value: 'value_one', label: 'ABC'},
{id: 'id_one', value: 'value_one', label: 'ABC'},
{id: 'id_three', value: 'value_three', label: 'ABX'},
{id: 'id_two', value: 'value_two', label: 'ABY'},
{id: 'id_four', value: 'value_four', label: 'ABD'}
];
functiongetDistValues(arr) {
var hash = {};
var uniqueArr = [];
for (var i = 0, l = arr.length; i < l; i++) {
if (hash[arr[i].id] === undefined) {
hash[arr[i].id] = 1;
uniqueArr.push(arr[i]);
}
}
return uniqueArr;
}
console.log(getDistValues(arr));
.as-console-wrapper { max-height: 100%!important; top: 0; }
Solution 4:
You can use reduce()
method for array with Object.values
for filter duplicates
const inputArray = [
{id: 'id_one', value: 'value_one', label: 'ABC'},
{id: 'id_one', value: 'value_one', label: 'ABC'},
{id: 'id_three', value: 'value_three', label: 'ABX'},
{id: 'id_two', value: 'value_two', label: 'ABY'},
{id: 'id_four', value: 'value_four', label: 'ABD'}
]
constfilterArray = (arr) => Object.values(arr.reduce(
(acum, item) => {
acum[item.id] = item
return acum
},
{})
)
console.log(filterArray(inputArray))
Solution 5:
You are adding items to the tempIndex
array before you check if they are there. Since you always add each, then it's implicit that when you do the check the item will already be there.
You have to inverse this - check if an item exists and then add it to tempIndex
var data = [
{id: "id_one", value: "value_one", label: "ABC"},
{id: "id_one", value: "value_one", label: "ABC"},
{id: "id_three", value: "value_three", label: "ABX"},
{id: "id_two", value: "value_two", label: "ABY"},
{id: "id_four", value: "value_four", label: "ABD"}
];
functiongetDistValues(object) {
var distObject = [];
var tempIndex = [];
var length = object.length;
for (var i = 0; i < length; i++) {
//check firstvar notSeen = tempIndex.indexOf(object[i].id) === -1;
if (notSeen) {
tempIndex.push(object[i].id);
//add later
distObject.push(object[i]);
}
}
return distObject;
}
var result = getDistValues(data);
console.log(result);
In order to be more efficient, you can also use an object to keep the duplicate IDs - that will eliminate the need for another iteration over tempIndex
every loop:
var data = [
{id: "id_one", value: "value_one", label: "ABC"},
{id: "id_one", value: "value_one", label: "ABC"},
{id: "id_three", value: "value_three", label: "ABX"},
{id: "id_two", value: "value_two", label: "ABY"},
{id: "id_four", value: "value_four", label: "ABD"}
];
functiongetDistValues(object) {
var distObject = [];
//use objectvar tempIndex = {};
var length = object.length;
for (var i = 0; i < length; i++) {
var notSeen = tempIndex[object[i].id] !== true;
if (notSeen) {
//mark as visited
tempIndex[object[i].id] = true;
distObject.push(object[i]);
}
}
return distObject;
}
var result = getDistValues(data);
console.log(result);
Post a Comment for "Javascript: Remove Duplicates In Array Of Objects Using A Single Property"