jQuery 去除重複資料
阿新 • • 發佈:2018-11-26
工作中肯定會碰見從介面中獲取到的多條資料一樣,如果在後臺可以處理,儘量在後臺處理掉,避免前臺處理重複資料(使用者是可以修改你的js檔案的),下面說的是從介面中直接獲取到的資料(當然了,這也不是我們自己提供的介面),一下是我個人的程式碼操作,一個菜鳥的理解:
1.首先寫一個去重的公共方法:
Array.prototype.clearRepeat = function () { var res = []; var json = {}; $.each(this,function (i,item) { //this 表示當前待處理資料,this可能是一個集合或是一個單獨欄位 if (!json[this.你需要的欄位]) { res.push(this); json[this.你需要的欄位] = 1; } }); return res; };
2.資料來源
把得到的資料可以放到data中
var data=[]; data.push('zhangsan'); data.push('zhangsan'); data.push('zhangsan'); data.push('zhangsan'); data.push('zhangsan');
3.呼叫清除重複程式
var datas=data.clearRepeat();
datas得到的是去重後的資料內容,可以在頁面展示或做其他處理。有什麼問題可以留言