Home > Web Front-end > JS Tutorial > body text

How to remove duplicate data from an array in jquery

coldplay.xixi
Release: 2020-11-30 16:02:21
Original
6538 people have browsed it

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.

How to remove duplicate data from an array in jquery

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);
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template