Detailed explanation of JavaScript array deduplication

Detailed explanation of JavaScript array deduplication

1. Array deduplication

      /**************************************************
         ╚description:
        ╚Author: Qilin Society╚Time: 2021-09-13 22:26:21
        ╚Name: V1.0.5
        ***************************************************/
        var obj = ['Qilin','She','CC','DD','Qilin','She','11',11]
            //Define a new array var s = [];
            //Traverse the array for(var i=0;i<obj.length;i++){
            if(s.indexOf(obj[i]) == -1){ //Judge whether it exists in the s array, if not, push it into the s array s.push(obj[i]);
             }
            }
        console.log(s); 

2. Deduplication of objects in arrays

       /**************************************************
         ╚description:
         ╚Author: Qilin Society╚Time: 2021-09-13 22:26:21
         ╚Name: V1.0.5
        ***************************************************/
        var old_data = [
            { name:'ccc', age:'18' },
            { name:'peng', age:'18' }, //Remove duplicate peng { name:'aaa', age:'18' },
            { name:'peng', age:'18' },
          ]
          // Method 1: Use the object access attribute method to determine whether the key exists in the object
          var result = [];
          var obj = {};
          old_data.forEach(function (data) {
          if(!obj[data.name]){
          result.push(data);
          obj[data.name] = true;
            }
          })
          console.log(result); 

3. Modify the value of another field based on the same field in the array

      /**************************************************
        ╚description:
        ╚Author: Qilin Society╚Time: 2021-09-13 22:26:21
        ╚Name: V1.0.5
        ***************************************************/
        var oldData = [
            { name:'cccc', age:'5656' },
            { name:'cccc', age:'22dddsada' },
            { name:'cccc', age:'22dddsada' },
            { name:'aaaa', age:'32' },
            { name:'aaaa', age:'2dasdasdas2' },
          ]
          var newArr = [];
          for (var i = 0; i < oldData.length; i++) {
          var item = oldData[i];
          var isExists = false;
          for (var j = 0; j < newArr.length; j++) {
          var item2 = newArr[j];
          if (item2.name == item.name) {
          isExists = true;
          break;
                }
            }
          if (isExists) {
          // Find the same here, change the same if(item.name == 'cccc'){
          item.age = '222222'
          item2.age = '222222'
                }else{
          item.age = '3333'
          item2.age = '3333'
                }
          newArr.push(item2);
          continue;
            }
          newArr.push(item);
          }
          console.log(newArr) 

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Five common ways to remove duplicate arrays in JavaScript
  • Introduction to JavaScript array deduplication and flattening functions
  • JS array deduplication details
  • Detailed discussion of several methods for deduplicating JavaScript arrays
  • JavaScript array deduplication solution
  • Seven ways to implement array deduplication in JS

<<:  Basic syntax of MySQL index

>>:  How to switch directories efficiently in Linux

Recommend

Detailed explanation of the difference between alt and title

These two attributes are often used, but their di...

Introduction to the role of HTML doctype

Document mode has the following two functions: 1. ...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...

Use pure JS to achieve the secondary menu effect

This article example shares the specific code of ...

Does Mysql ALTER TABLE lock the table when adding fields?

Table of contents Before MySQL 5.6 After MySQL 5....

Detailed explanation of MySQL phantom reads and how to eliminate them

Table of contents Transaction Isolation Level Wha...

Creative About Us Web Page Design

Unique “About”-Pages A great way to distinguish yo...

MySQL 8.X installation tutorial under Windows

I had been using MySQL 5.7 before, but because My...

Why web page encoding uses utf-8 instead of gbk or gb2312?

If you have a choice, you should use UTF-8 In fac...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

The benefits of div+css and web standard pages

The div element is used to provide structure and b...