This article mainly introduces the javascript array deduplication method, and compares and analyzes the principle and Operation Skills of javascript Array deduplication through two examples, for more information about how to deduplicate javascript arrays, see the example in this article. We will share this with you for your reference. The details are as follows:
Method 1.
Idea: Create a New empty array, traverse the old array cyclically, and use the indexOf () method to obtain the position of the element in the array. If the value is-1, it indicates that the element does not exist. Then, the new array uses indexOf to obtain each element of the old array. If the value is-1, it indicates that the element does not exist. Then, it pushes the element to the new array and outputs the new array, that is, the de-duplicated array.
Var arr = [24, 56,]; var new_arr = []; for (var I = 0; IMethod 2.
Var arr = [, 49];
Var o = {}; var new_arr = []; for (var I = 0; IThe same principle is to use an empty object to determine
Note: The second method is to store existing objects into an object in the form of a subscript reference, which is much faster than searching arrays with indexOf, therefore, the first method consumes resources.
I hope this article will help you design JavaScript programs.
For more articles on de-duplicated javascript array analysis, refer to PHP Chinese network!