We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function unique(arr) { var ret = []; for (var i = 0; i < arr.length; i++) { var item = arr[i]; if (ret.indexOf(item) === -1) { ret.push(item); } } return ret; }
var indexOf = [].indexOf ? function (arr, item) { return arr.indexOf(item) } : function indexOf(arr, item) { for (var i = 0; i < arr.length; i++) { if (arr[i] === item) { return i } } return -1 } function unique(arr) { var ret = [] for (var i = 0; i < arr.length; i++) { var item = arr[i] if (indexOf(ret, item) === -1) { ret.push(item) } } return ret }
function unique(arr) { var ret = []; var hash = {}; for (var i = 0; i < arr.length; i++) { var item = arr[i]; var key = typeof (item) + item; if (hash[key] !== 1) { ret.push(item); hash[key] = 1; } } return ret; }
function unique(arr) { return [...new Set(arr)]; }
function unique(arr) { let ret = []; let obj = {}; for (var i = 0; i < arr.length; i++) { let item = arr[i]; if (!obj[item]) { ret.push(item); obj[item] = 1; } } return ret; }
JS去重的方法有非常多种,以上的5种仅是代表不同的差异。我觉得需要结合实际项目需求开发,根据需求来选择哪种去重方法,根据需求来修改某些条件,或者增加条件,适合需求的才是最好的去重方法。
最简单的,我不用适配低版本浏览器,也不考虑严格的数据类型值,直接用 ...new Set(arr) 最快啦~
The text was updated successfully, but these errors were encountered:
lfb
No branches or pull requests
方法一
方法二
方法三
方法四
方法五
思考
JS去重的方法有非常多种,以上的5种仅是代表不同的差异。我觉得需要结合实际项目需求开发,根据需求来选择哪种去重方法,根据需求来修改某些条件,或者增加条件,适合需求的才是最好的去重方法。
最简单的,我不用适配低版本浏览器,也不考虑严格的数据类型值,直接用 ...new Set(arr) 最快啦~
The text was updated successfully, but these errors were encountered: