javascript - jQuery 2.1 | Remove source substring of duplicate substring -
i wish preserve following (as opposed preceding) duplicated substring of comma-delimited string while removing preceding duplicating substring.
1- initial state of string before duplicate appended:
aaa,bbb,ccc,ddd,eee
2- bbb dynamically appended string:
aaa,bbb,ccc,ddd,eee,bbb
3- preceding bbb must removed:
aaa,ccc,ddd,eee,bbb
how can following function, or function matter, reproduce seek?
function unique(list) { var result = []; $.each(list, function(i, e) { if ($.inarray(e, result) == -1) { result.push(e); } }); return result; }
the quick , easy way reverse array, use duplicate removal function , reverse back:
function unique(list) { var result = []; $.each(list, function(i, e) { if ($.inarray(e, result) == -1) { result.push(e); } }); return result; } var arr = ["aaa", "bbb", "ccc", "ddd", "eee", "bbb"]; console.log(unique(arr.reverse()).reverse());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
there ways better performance, relatively small array should fine.
you mention string (though function works on arrays). if string, need use split
break array , join
after duplicate removal string.
Comments
Post a Comment