Home > Web Front-end > JS Tutorial > How to implement deduplication of array attributes and verify duplicate data in js

How to implement deduplication of array attributes and verify duplicate data in js

王林
Release: 2020-03-27 10:19:25
forward
3140 people have browsed it

How to implement deduplication of array attributes and verify duplicate data in js

New task: drop-down box to remove duplicate data

//Remove the front-end object array according to a certain attribute

The jsonArray is the one you want to duplicate array of objects.

Recommended tutorial: js tutorial

In the example, duplicates are removed according to the name attribute, as follows:

//前端对象数组 按某个属性去重
     var obj = {};
    jsonArray = jsonArray.reduce(function(item,next){
      obj[next.name]?'':obj[next.name] = true&&item.push(next);
      return item;
    },[]);       
    for(var i= 0;i<jsonArray.length;i++){
        console.log(jsonArray[i])
    }
Copy after login

Verify whether a certain attribute in the object array exists Repeat

Check the double if loop in the array object array to check whether the identityid2 attribute is repeated

if(json[i][key].length>0){
                var array = json[i][key];
                 for(var x =0 ;x<array.length;x++){
                    for(var y =0;y<array.length;y++){
                      if(array[x].identityid2 == array[y].identityid2 && x!=y){
                        sweetAlert("","待签结算合同"+ i +"中的未中标供应商的第" + (x+1) +"包的身份证号" + array[x].identityid2 + "被复用", "error");
                        return false;
                      }
                    }                                      
                 }
               }
Copy after login

The above is the detailed content of How to implement deduplication of array attributes and verify duplicate data in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
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