Not much, the code for removing duplicate data from js array is as follows:
var arr = [1,2,3,4,5,6,1,6,7,2];var newArr = [];for(var i =0;i<arr.length-1;i++){ if(newArr.indexOf(arr[i]) == -1){ newArr.push(arr[i]); }}Let's share with you the efficient removal of duplicates in js array
The Array type does not provide a method to repeat. If you want to kill the duplicate elements of the array, you have to think of a solution:
function unique(arr) {var result = [], isRepeated;for (var i = 0, len = arr.length; i < len; i++) {isRepeated = false;for (var j = 0, len = result.length; j < len; j++) {if (arr[i] == result[j]) { isRepeated = true;break;}}if (!isRepeated) {result.push(arr[i]);}}return result;}The overall idea is to transport the array elements to another array one by one, and check whether this element is duplicated during the transfer process, and if there is one, throw it away directly. As can be seen from the nested loop, this method is extremely inefficient. We can use a hashtable structure to record existing elements, so that inner loops can be avoided. Just so happens that it is extremely simple to implement hashtable in Javascript, and is improved as follows:
function unique(arr) {var result = [], hash = {};for (var i = 0, elem; (elem = arr[i]) != null; i++) {if (!hash[elem]) {result.push(elem);hash[elem] = true;}}return result;//http://www.cnblogs.com/sosoft/}The above is the implementation code for removing duplicate data from JS arrays introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!