Home >Web Front-end >JS Tutorial >How to remove duplicate data from an array in jquery
How to deduplicate data in an array in jquery: First use a loop to traverse the array, write it into a new array, and use the [$.inArray()] method to determine whether the new array already exists before writing. There is this data; then if it is not written again, the new array obtained by this method will remove the duplicate data.
The operating environment of this tutorial: Windows 7 system, jquery version 3.2.1. This method is suitable for all brands of computers.
How to remove duplicate data from arrays in jquery:
Use a loop to traverse the array in jquery, write it into a new array, and use it before writing $.inArray()
The method determines whether the data already exists in the new array. If it is not written again, the new array obtained by this method will remove duplicate data.
$.inArray()
The function is used to find the specified value in the array and return its index value (if not found, return -1)
for(var i=0;i<arr.length;i++) { var items=arr[i]; //判断元素是否存在于new_arr中,如果不存在则插入到new_arr的最后 if($.inArray(items,new_arr)==-1) { new_arr.push(items); } }
Related free learning recommendations: javascript (video)
The above is the detailed content of How to remove duplicate data from an array in jquery. For more information, please follow other related articles on the PHP Chinese website!