Array judgment
Before talking about how to deduplicate and level the array, let’s talk about how to judge the array, because to process the array, of course, you must first determine whether the data passed is an array.
First of all, we all know that there are only 5 types of js data types, namely Undefined, Null, Boolean, Number and String. The array is just an object.typeof([])
The returned result is a Object string, so we need to judge it through other means. Here are two methods.
The first method of using instanceof
instanceof is a method provided by ES5. It can be used to determine whether an instance is an instance of a certain class, for example:
[] instenceof Array //The result istrue
The disadvantage of this method is that it has poor compatibility, and it will be confused for some low-version browsers that do not support ES5.
The second method is to judge through the prototype chain
If you understand js, you should understand that the characteristic of the js language is prototype chain, and all objects are inherited from, and there is another prototype
toString()
Method, thistoString()
What is the method used for? It is to return the value of the current object as a string. I may not understand this sentence for the first time. For example:
var num = 123; (); //The result is"123"
Have you understood it? It is the string form that returns the value of the object num, that is, "123". OK, what does this have to do with judging arrays? Think about all objects inherit from, so is an array, if you send an array to
as a "value" in the call
toString()
Method, then it should display the name of this object. This is the principle of judgment. The code is as follows:
([]); //turn out"[object Array]"
For script libraries like jQueryisArray()
This is the method used.
Array flattening
After saying that, I went straight to the topic, and first I took the array flattening. What is the array flattening? It is to spread [1,[2,[3,4],5]] into [1,2,3,4,5]. I have two ideas about array shooting. The second one is quite weird. Let’s leave some suspense haha.
The first is the conventional idea
Traverse the array. If the array is wrapped in the array, continue to traverse the inside until each element is traversed, and then traverse it while stuffing it into the new array variable. This way, the flattening is completed. The specific code is as follows:
panelArr = function(arr){ var newArr = []; var isArray = function(obj) { return (obj) === '[object Array]'; }; var dealArr = function(arr){ for (var i = 0;i<;i++){ isArray(arr[i]) ? dealArr(arr[i]) : (arr[i]); } }; dealArr(arr); return newArr; }; (panelArr([1,[2,3]])); //[1,2,3]
Of course, this method can also be written inIt is more convenient to use. One problem with this method is memory usage, because if the data volume is large, it will take up a lot of memory.
The second strange idea
The second idea is to not look at the array, nor do you traverse it and directly shoot the flat. It sounds a bit strange, how can you shoot flat without traversing it? Just usejoin()
Method: convert the array into a string, then remove the symbols regularly and finally merge. Please note that this method cannot be used.join("")
, because if split like this, will 13 be 1 and 3 or 13? It's hard to distinguish, the code is as follows:
var arr = [1,2,[33,43],20,19]; (".").replace(/,/g,".").split("."); //["1", "2", "33", "43", "20", "19"]
Notice:This method converts the data type into a string.
Array deduplication
Below is the array deduplication, for example, [1, 2, 3, 3, 4, 5, 5, 5, 6] becomes [1, 2, 3, 4, 5, 6]. The core of this implementation is to deduplicate it here. If you can quickly judge whether the elements are repeated, it is the key.
Still two ways of thinking
The first traversal idea
It is to prepare a new array variable, and traverse the variable every time before stuffing it in to see if there are any duplications. If not, stuff it in. The final new array is the array after deduplication. The sample code is as follows:
function uniqueArr(arr){ var newArr = []; (arr[0]); for(var i = 1; i<;i++){ var repeat = false; for(var j = 0;j<;j++){ if(arr[i] == newArr[j]){ repeat = true; } } if(!repeat){ (arr[i]); } } return newArr; }
The second type of hash judgment
The time complexity above isO(n^2)
The method is not a good method. Its bottleneck is to judge whether to repeat it here, so we change to a more efficient method of retrieving whether it is repeated. This method is hashing. Why is hashing the fastest? Let’s look at the data structure, I won’t go into details here.
The idea of this method is to add a hash filter between the original array and the deduplicated array. In general, the original array data is handed over to the hash to see if there is any duplication. If there is no, add it. The specific code is as follows:
function uniqueArr(arr){ var newArr = [], hashFilter = {}; for(var i = 0;i<;i++){ if(!hashFilter[arr[i]]){ //If there is no existence, change the corresponding value of this property to true and stuff it into the deduplication array hashFilter[arr[i]] = true; (arr[i]); } } return newArr; }
I prefer the second type because it is really quick to judge whether to repeat it here, and it can be said that it comes out in seconds.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.