I've been working on finding and removing duplicate objects in an array, but I keep encountering an error when trying to access the filterList[i+1].tagID element. Strangely, manually inputting the [i+1] values seems to yield the correct results. I'm puzzled as to why the i+1 reference is causing an issue. Additionally, I've been considering whether using a slice[i, 1] method would be more effective than delete.
const filterList = [{
tagID: 1,
tagName: "Red"
}, {
tagID: 1,
tagName: "Red"
}, {
tagID: 2,
tagName: "Orange"
}, {
tagID: 2,
tagName: "Orange"
}, {
tagID: 5,
tagName: "Blue"
}, {
tagID: 5,
tagName: "Blue"
}, {
tagID: 5,
tagName: "Blue"
}, {
tagID: 6,
tagName: "Indigo"
}, {
tagID: 6,
tagName: "Indigo"
}, {
tagID: 7,
tagName: "Violet"
}, {
tagID: 7,
tagName: "Violet"
}, {
tagID: 7,
tagName: "Violet"
}]
filterList.sort(function(a, b) {
return a.tagID - b.tagID;
});
for (let i = 0; i < filterList.length; i++) {
if (filterList[i].tagId == filterList[i+1].tagID) {
delete filterList[i];
}
}
console.log(filterList)