I have an array with duplicates:
nameList = [{name:"name1", filename:"filename1"}, {name:"name2", filename:"filename2"}, {name:"name3", filename:"filename2"}]
and I want to retain only the unique item but the last item only. I want:
nameList = [{name:"name1", filename:"filename1"}, {name:"name3", filename:"filename2"}]
I have tried:
var flags = {};
resultNameList = nameList.filter(function(entry) {
if (flags[entry.filename]) {
return false;
}
flags[entry.filename] = true;
return true;
});
but it retains only the first occurrence of the duplicate. How can I get the last occurrence?
Please help. Thanks.
file. In rest of the object it isfilename. Please confirm which one is correct