How does jquery define empty arrays
This article mainly explains "how jquery defines empty arrays". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how jquery defines empty arrays" together!
First define the following array:
var arr=[0,2,3,5,6,9,2];
We can see that there is a duplicate element '2' in the array;
Finally, jquery filtering should get [0,2,3,5,6,9];
OK, first we define an empty array to store the last filtered elements:
var new_arr=[];
Using jquery's $.inArray can be easily implemented:
Traversing array elements:
for(var i=0;i
var items=arr[i];
//Determine whether the element exists in new_arr, if not insert it to the end of new_arr
if($.inArray(items,new_arr)==-1) {
new_arr.push(items);
}
}
When traversing to the last '2', because the previous '2' has been inserted into new_arr, the last '2' will not execute the statement in if.
Finally, we get the new array new_arr=[0,2,3,5,6,9];
Note: $.inarray(value,array) --Determines the position of the first argument in the array (returns-1 if not found).
The final code is as follows:
var arr=[0,2,3,5,6,9,2];
var new_arr=[];
for(var i=0;i
var items=arr[i];
//Determine whether the element exists in new_arr, if not insert it to the end of new_arr
if($.inArray(items,new_arr)==-1) {
new_arr.push(items);
}
}
console.log(new_arr); //[0,2,3,5,6,9]
Thank you for reading, the above is "jquery how to define empty array" content, after the study of this article, I believe we have a deeper understanding of jquery how to define empty array this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!