基本思路,对象的属性名不能重复(注意不是属性值),重复的话覆盖。所以得到这样的代码:
var str = "1,2,4,5,2,4"; // document.getElementById("textbox1").innerText;
var s = str.split(',');
var dic = {};
for (var i = s.length; i--; ) {
dic[s[i]]=s[i];
}
var r = [];
for (var v in dic) {
r.push(dic[v]);
}
alert(r.join());// -----------1,2,4,5