Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does jquery define empty arrays

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report