Using JavaScript difference to implement a comparison tool

Using JavaScript difference to implement a comparison tool

Preface

At work, I need to count the materials submitted by employees every week, but I don’t want to copy and paste them one by one, so I have to write a small tool to help me find who has not submitted the materials.

Let’s fix the page first

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    textarea {
      /* border: none; */
      width: 49%;
      height: 400px;

      /* font-size: 17pt; */

    }
    #btn {
      width: 100%;
      height: 50px;
      position: relative;
      top: 0px;
      /* position: absolute; */
    }

    #p2 {
      margin-left: 940px;
      margin-top: -38px;
    }
  </style>
</head>
<body>
  <button id="btn" class="ambi-light-button">Compare</button>
  <textarea id="txt" type="text" placeholder="Should be submitted"></textarea>
  <textarea id="txt2" type="text" placeholder="Submitted"></textarea>
  <hr>
  <p>Not submitted</p>
  <p id="p2">List of absent persons has been submitted</p>
  <textarea id="txt3" type="text" placeholder="Not submitted"></textarea>
  <textarea id="txt4" type="text" placeholder="Submitted list of people not on the list"></textarea>
</body>
</html>

A bit ugly, but it doesn't matter if you use it yourself

Start writing JS code

<script
 //First get the input box and button let txt = document.querySelector('#txt')
  let txt2 = document.querySelector('#txt2')
  let txt3 = document.querySelector('#txt3')
  let txt4 = document.querySelector('#txt4')
  let btn = document.querySelector('#btn')
 //Then write an array to find the difference const getDifference = function (a, b) {
   //Explanation: If the two functions passed in are arrays if (a.constructor === Array && b.constructor === Array) {
      let set1 = new Set(a);
      let set2 = new Set(b);
      // Use Set to remove duplicates and filter to find the difference return Array.from(new Set([...set1].filter(x => !set2.has(x))));
    }
    return null;
  }
  // Simply give the button a click event btn.onclick = function () {
    //List of people who should submit let Should_sub = txt.value.split('\n')
    //List of people who have not submitted yet let already_sub = txt2.value.split('\n')
    let l3 = getDifference(Should_sub, already_sub)
    //Number of people not submitted in the list let l4 = getDifference(already_sub, Should_sub)
    //The filtered values ​​are fed back to the two input boxes on the page txt3.value = l3.join('\n')
    txt4.value = l4.join('\n')
  }
  </script>

Summarize

This is the end of this article about using JavaScript difference to implement a comparison widget. For more relevant JS difference implementation comparison widget content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use node.js to develop a small tool for generating frame-by-frame animation
  • Node.js implements ticket grabbing gadget & SMS notification reminder function
  • Java9's JShell gadget and compiler two automatic optimization methods
  • Node.js implements JS file merging tool
  • JavaScript makes a small tool to convert sql to stringBuffer
  • Theory of creating a bookmarklet with js
  • A small tool for scheduled restart or shutdown written in hta[javascript]
  • The National Day is coming. Use JS to implement a small tool to generate a National Day style avatar. Detailed explanation of the implementation process

<<:  How to use Nginx to proxy multiple application sites in Docker

>>:  MySQL 5.7.23 decompression version installation tutorial with pictures and text

Recommend

Solution to PHP not being able to be parsed after nginx installation is complete

Table of contents Method 1 Method 2 After install...

npm Taobao mirror modification explanation

1. Top-level usage 1. Install cnpm npm i -g cnpm ...

How to optimize the slow Like fuzzy query in MySQL

Table of contents 1. Introduction: 2. The first i...

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

Learn Vue middleware pipeline in one article

Often when building a SPA, you will need to prote...

Summary of Mysql slow query operations

Mysql slow query explanation The MySQL slow query...

The Complete Guide to Grid Layout in CSS

Grid is a two-dimensional grid layout system. Wit...

Add crontab scheduled tasks to debian docker container

Now most of the Docker images are based on Debian...

Common causes and solutions for slow MySQL SQL statements

1. Slow query due to lack of index or invalid ind...

HTML meta explained

Introduction The meta tag is an auxiliary tag in ...