I have an array which has only objects in it and I would like to remove duplicated objects based one of their key value which is val.js.url
but wanna make it remaining object the latest one among the duplicated objects so I basically used reverse()
in my code`.
My array:
[
{
"id": "js_1oc3uiteki22",
"tab": {
"title": "title 1",
"url": "http://google.com"
},
"js": {
"initiator": "https://google.com",
"url": "http://url.com"
}
},
{
"id": "js_1oc3uiteki22",
"tab": {
"title": "title 2",
"url": "http://google2.com"
},
"js": {
"initiator": "https://google2.com",
"url": "http://url.com"
}
}
]
Expected result:
[
{
"id": "js_1oc3uiteki22",
"tab": {
"title": "title 2",
"url": "http://google2.com"
},
"js": {
"initiator": "https://google2.com",
"url": "http://url.com"
}
}
]
Actually I have this following code which solves this problem but when I need to use that code 2 times in same js file I guess it's kinda conflicts and doesn't clean the duplicates well in second array.
function dupfix(arr) {
return arr.reverse().filter(val => {
if (!this.hasOwnProperty(val.js.url)) {
return this[val.js.url] = true
}
return false
}, {}).reverse()
}
file = dupfix(file)
So, how can I fix that code above or is there any other better methods to do that? Thanks.