This article describes the JavaScript method of removing duplicates from arrays based on objects. Share it for your reference, as follows:
In JavaScript, removing array duplicates is a very common function, and it is often asked during interviews. Many people usually use multi-layer for loops to compare step by step and then delete them. This not only has a lot of code, but also has poor performance. In JavaScript objects, there is a feature that the key will never be repeated. If the subsequent one is repeated, the previous one will be overwritten.
Three steps:
1# Convert array to js object
2# Turn array value into key in js object
3# Restore the object to an array
var toObject = function(arr){ var obj = new Object(); //Private object var j = ; for(var i=0; i < j; +i+){ obj[arr[i]] = true; } return obj; } var keys = function(obj){ var arr = []; for(var attr in obj){ if((attr)){ (attr); } } return arr; } var uniq = function(arr){ //Remove duplicates return keys(toObject(newarr)); }
When using it, just pass the array into the uniq function. This method uses JavaScript object characteristics, which is very efficient and concise. It is also the underlying implementation of Yahoo YUI.
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript array operation skills》、《JavaScript traversal algorithm and skills summary》、《Summary of JavaScript mathematical operations usage》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation special effects and techniques"and"Summary of JavaScript Errors and Debugging Skills》
I hope this article will be helpful to everyone's JavaScript programming.