search
HomeWeb Front-endJS TutorialOrganize and share 5 common data processing problems in JavaScript development

Organize and share 5 common data processing problems in JavaScript development

With the continuous development of front-end technology, the interfaces that need to be displayed in front-end work are becoming more and more complex, so there are more and more data processing scenarios. For example: back-end management systems often need to display A tree structure, the front-end data returned by the background is a horizontal structure. At this time, we need to convert the data into a tree structure; when displaying the echart histogram, the returned data needs to be deduplicated and merged; when filtering, we The data needs to be sorted; the most common ones are the addition, deletion, modification and checking of Dom when we leave messages and comments, etc. So today's article will take you into these business scenarios and face these difficult problems head-on. Let's face them No longer be afraid of JavaScript data operations, making development work simpler and more efficient.

1. Add, delete, modify and check data

Scenario: This is a background management system - the dictionary management module, which includes four operations of adding, deleting, modifying and checking the data dictionary. So what is our solution to deal with these 4 operations? Please read on below

1. New additions to the array

arr.push Push one or more elements from the back of the array

var arr = [1,2,3];
// 返回:修改后数组的长度
arr.push(4,5,6);
console.log(arr)
//输出结果
arr=[1,2,3,4,5,6]

arr.unshift Add one or more elements from the front of the array

var arr = [1,2,3];
// 返回:修改后数组的长度
arr.unshift(4,5,6);
console.log(arr)
//输出结果
arr=[4,5,6,1,2,3]

2. Array deletion

arr.shift is used to remove the first element of the array

// 数组的shift方法用于将数组的第一个元素移除
var arr = [1,2,3];
// 返回 被删除的元素;
arr.shift();
//输出结果
arr=[2,3]

arr.pop Delete the last element of the array;

// 数组的pop方法用于将数组的最后一个元素移除
var arr = [1,2,3];
// 返回 被删除的元素;
arr.pop();
//输出结果
arr = [1,2];

3. Modification of the array

arr.splice: Yes Add, delete, and modify any position of the array Then an empty array is returned))

Syntax

splice(index,howmany,item1,…itemx);

index—— Must be an integer that specifies the position to add or delete. Use a negative number to specify the position from the end of the array.

    howmany——Required, the quantity to be deleted, if it is 0, the item will not be deleted.
  • item1,…itemx – Optional, new items to add to the array.
1. 删除
可删除任意数量的项,只需指定2个参数:要删除的第一项的位置和要删除的项数。

let arr=[1,2,3];
let arr1=arr.splice(1,2);//会删除数组的第2和3个元素(即2,3)
alert(arr);//[1]
alert(arr1);//[2,3]


2. 插入
可以向指定位置插入任意数量的项只需提供3个参数:起始位置、0(要删除的项数)、要插入的项。
let arr=[1,2,3];
let arr1=arr.splice(1,0,4,5);//会从数组的1位置开始插入4,5
alert(arr);//[1,4,5,2,3]
alert(arr1);//[]

3. 替换
可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定3个参数:起始位置、要删除的项数和要插入的任意数量的项(插入的数量不必与删除的数量相等)
let arr = [1,2,3];
let arr1=arr.splice(1,1,"red","green");//会删除2,然后从2位置插入字符串"red"和"green"
alert(arr);//[1,"red","green",3]
alert(arr1);//[2]
  • 4. Array search

    arr.indexOf

    : Find the index based on the element. If the element is in the array, return Index, otherwise -1 is returned to find out whether the element is inside the array

    var arr = [10,20,30]
    console.log(arr.indexOf(30));  // 2
    console.log(arr.indexOf(40));  // -1
    arr.findIndex: Used to find the index of the first element that meets the condition, if not, return -1

    var arr = [10, 20, 30];
    var res1 = arr.findIndex(function (item) {
      return item >= 20;
    });
    // 返回 满足条件的第一个元素的的索引
    console.log(res1);
    5. Conversion between arrays and strings

    #join

    is used to join multiple elements in the array into one character using the specified delimiter. String

    var arr = ['用户1','用户2','用户3'];
    var str = arr.join('|'); 
    console.log(str);  //  
    用户1|用户2|用户3
    split String method: convert numbers, followed by separated characters

    // 这个方法用于将一个字符串以指定的符号分割成数组
    var str = '用户1|用户2|用户3';
    var arr = str.split('|');
    console.log(arr);
    ['用户1','用户2','用户3']
    2. Sorting of data

    It has to be said that with the advancement of technology and the development of hardware, the computing performance of browsers has also improved. Next we will encounter the second situation-the sorting operation of data, which requires us to implement various operations on the front end. kind of sorting, so what are our solutions? Let’s look down~

    1. js’s own function arr.sort()

     var arr = [23,34,3,4,23,44,333,444];
            arr.sort(function(a,b){
                return  a-b;
            })
        console.log(arr);
    Here we also introduce several commonly used sorting algorithms:

    2、插入排序

    var arr = [23,34,3,4,23,44,333,444];
    
    var arrShow = (function insertionSort(array){
    if(Object.prototype.toString.call(array).slice(8,-1) ==='Array'){
    
        for (var i = 1; i = 0 && array[j] > key) {
            array[j + 1] = array[j];
            j--;
            }
          array[j + 1] = key;
        }
        return array;
    }else{
    
        return 'array is not an Array!';
    }
    })(arr);
    
    console.log(arrShow);//[3, 4, 23, 23, 34, 44, 333, 444]

    3、二分插入排序

    function binaryInsertionSort(array) {
    if (Object.prototype.toString.call(array).slice(8, -1) === 'Array') {
    for (var i = 1; i = left; j--) {
        array[j + 1] = array[j];
    }
    array[left] = key;
    }
    return array;
    } else {
        return 'array is not an Array!';
    }
    }

    4、选择排序

    function selectionSort(array) {
    if (Object.prototype.toString.call(array).slice(8, -1) === 'Array') {
        var len = array.length, temp;
        for (var i = 0; i <h3 id="strong-冒泡排序-strong"><strong>5、冒泡排序</strong></h3><pre class="brush:php;toolbar:false">function bubbleSort(array) {
    if (Object.prototype.toString.call(array).slice(8, -1) === 'Array') {
    var len = array.length, temp;
    for (var i = 0; i = i; j--) {
            if (array[j] <h3 id="strong-快速排序-strong"><strong>6、快速排序</strong></h3><pre class="brush:php;toolbar:false">//方法一
    function quickSort(array, left, right) {
    if (Object.prototype.toString.call(array).slice(8, -1) === 'Array' && typeof left === 'number' && typeof right === 'number') {
    if (left <h3 id="strong-堆排序-strong"><strong>7、堆排序</strong></h3><pre class="brush:php;toolbar:false">/*方法说明:堆排序
    @param array 待排序数组*/
    function heapSort(array) {
    if (Object.prototype.toString.call(array).slice(8, -1) === 'Array') {
    //建堆
    var heapSize = array.length, temp;
    for (var i = Math.floor(heapSize / 2); i >= 0; i--) {
    heapify(array, i, heapSize);
    }
    
    //堆排序
    for (var j = heapSize - 1; j >= 1; j--) {
        temp = array[0];
        array[0] = array[j];
        array[j] = temp;
        heapify(array, 0, --heapSize);
    }
    } else {
    return 'array is not an Array!';
    }
    }
    /*方法说明:维护堆的性质
    @param arr 数组
    @param x 数组下标
    @param len 堆大小*/
    function heapify(arr, x, len) {
    if (Object.prototype.toString.call(arr).slice(8, -1) === 'Array' && typeof x === 'number') {
    var l = 2 * x, r = 2 * x + 1, largest = x, temp;
    if (l  arr[largest]) {
    largest = l;
    }
    if (r  arr[largest]) {
    largest = r;
    }
    if (largest != x) {
        temp = arr[x];
        arr[x] = arr[largest];
        arr[largest] = temp;
        heapify(arr, largest, len);
    }
    } else {
        return 'arr is not an Array or x is not a number!';
    }
    }

    三、数据的去重

            好的,当我们解决完排序的问题,紧接着我们又面临着数据去重的问题,不要怕,解决方案依然有很多,请您慢慢往下接着看:

            在工作上,对json数据处理时,例如遇到对某些产品的尺码进行排序,不同的产品都有相同的尺码那是正常不过的事情,如果我们要把这些转成表格的形式来展现,那么这些尺码就不要不能重复才行.在这里呢,我就写几个数组去重的方法,给大家参考参考 :

     1、简单的去重方法

    // 最简单数组去重法
    /*
    * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中
    * IE8以下不支持数组的indexOf方法
    * */
    function uniq(array){
        var temp = []; //一个新的临时数组
        for(var i = 0; i <h3 id="strong-对象键值法去重-strong"><strong>2、对象键值法去重</strong></h3><pre class="brush:php;toolbar:false">/*
    * 速度最快, 占空间最多(空间换时间)
    *
    * 该方法执行的速度比其他任何方法都快, 就是占用的内存大一些。
    * 现思路:新建一js对象以及新数组,遍历传入数组时,判断值是否为js对象的键,
    * 不是的话给对象新增该键并放入新数组。
    * 注意点:判断是否为js对象键时,会自动对传入的键执行“toString()”,
    * 不同的键可能会被误认为一样,例如n[val]-- n[1]、n["1"];
    * 解决上述问题还是得调用“indexOf”。*/
    function uniq(array){
        var temp = {}, r = [], len = array.length, val, type;
        for (var i = 0; i <h3 id="strong-排序后相邻去除法-strong"><strong>3、排序后相邻去除法</strong></h3><pre class="brush:php;toolbar:false">/*
    * 给传入数组排序,排序后相同值相邻,
    * 然后遍历时,新数组只加入不与前一值重复的值。
    * 会打乱原来数组的顺序
    * */
    function uniq(array){
        array.sort();
        var temp=[array[0]];
        for(var i = 1; i <h3 id="strong-数组下标法-strong"><strong>4、数组下标法</strong></h3><pre class="brush:php;toolbar:false">/*
    *
    * 还是得调用“indexOf”性能跟方法1差不多,
    * 实现思路:如果当前数组的第i项在当前数组中第一次出现的位置不是i,
    * 那么表示第i项是重复的,忽略掉。否则存入结果数组。
    * */
    function uniq(array){
        var temp = [];
        for(var i = 0; i <h3 id="strong-优化遍历数组法-strong"><strong>5、优化遍历数组法</strong></h3><pre class="brush:php;toolbar:false">// 思路:获取没重复的最右一值放入新数组
    /*
    * 推荐的方法
    *
    * 方法的实现代码相当酷炫,
    * 实现思路:获取没重复的最右一值放入新数组。
    * (检测到有重复值时终止当前循环同时进入顶层循环的下一轮判断)*/
    function uniq(array){
        var temp = [];
        var index = [];
        var l = array.length;
        for(var i = 0; i <h2 id="strong-四-平级列表变成树形结构-strong"><strong>四 、平级列表变成树形结构</strong></h2><p>        呐,在选择部门的时候,是不是会经常看到这种树状菜单,后台返回的数据一般都是平级的数组,那么这种菜单,我们一般是怎么生成的呢,请看~~</p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/5a1c26c61be3b8f0fdb8276aa4bc4565-4.png?x-oss-process=image/resize,p_40" class="lazy" alt=""    style="max-width:90%"  style="max-width:90%"></p><h3 id="strong-这里特意将方法奉上-strong"><strong> 1、这里特意将方法奉上:</strong></h3><pre class="brush:php;toolbar:false">const dataTree = [
            {id: 1, name: '总公司', parentId: 0},
            {id: 2, name: '深圳分公司', parentId: 1},
            {id: 3, name: '北京分公司', parentId: 1},
           {id: 4, name: '研发部门', parentId: 2},
            {id: 5, name: '市场部门', parentId: 2},
            {id: 6, name: '测试部门', parentId: 2},
            {id: 7, name: '财务部门', parentId: 2},
            {id: 8, name: '运维部门', parentId: 2},
            {id: 9, name: '市场部门', parentId: 3},
            {id: 10, name: '财务部门', parentId: 3},
           
        ]
        function changeData(data, parentId = 0) {
            let tree = [];//新建空数组
            //遍历每条数据
            data.map((item) => {
                //每条数据中的和parentId和传入的相同
                if (item.parentId == parentId) {
                    //就去找这个元素的子集去  找到元素中parentId==item.id 这样层层递归
                    item.children = changeData(data, item.id);
                    tree.push(item);
                }
            })
            return tree
        }
        console.log(changeData(dataTree, 0));

    五、数组对象相同项合并处理

            我们在图表展示的时候会经常遇到数据处理,其中数组合并处理也会经常遇到,下面就是数组相同项合并的一种方式:

    • 首先由原始的数组arr数据,
    • 然后创建一个map空对象和一个result空数组,通过判断map中是否含有某项来判断数组result是否添加数据,
    • 然后再判断相同项和已有的result数组内容比较合并;

    1、博主特意将珍藏多年的祖传代码,在这里奉献给各位大佬:

      var arr = [
        {"id":"1","name":"车厘子","num":"245"},
        {"id":"1","name":"车厘子","num":"360"},
        {"id":"2","name":"苹果","num":"120"},
        {"id":"2","name":"苹果","num":"360"},
        {"id":"2","name":"苹果","num":"180"},
        {"id":"3","name":"香蕉","num":"160"},
        {"id":"4","name":"菠萝","num":"180"},
        {"id":"4","name":"菠萝","num":"240"}
      ];
      var map = {},result= [];
      for(var i = 0; i <p>看到这里 ,前端常见的几种数据处理的疑难杂症已经解决的差不多了,当然呐,现实情况下还有许许多多的问题未收录进来,后续会陆陆续续慢慢更新收录下来,同时呢也希望有遇到JavaScript数据处理比较头疼的朋友可以与博主交流探讨,有好的解题思路的也可以反馈给到博主。</p><hr><h2 id="strong-总结-strong"><strong>总结</strong></h2><p>       以上就是今天要讲的全部内容,本文介绍了JavaScript开发过程中常见的5种数据处理问题并提供了对应的解决思路,基本覆盖了日常开发过程中的使用需求, 阅读本片文章可以大大提升你的javaScript基本功,在收到开发需求时,能快速响应,并给出解决方案。</p><p>【相关推荐:<a href="https://www.php.cn/course/list/17.html" target="_blank" textvalue="javascript视频教程">javascript视频教程</a>、<a href="https://www.php.cn/course/list/1.html" target="_blank">web前端</a>】</p>

    The above is the detailed content of Organize and share 5 common data processing problems in JavaScript development. For more information, please follow other related articles on the PHP Chinese website!

  • Statement
    This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
    From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

    JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

    Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

    Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

    The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

    C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

    JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

    JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

    JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

    The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

    Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

    Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

    Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

    Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

    Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

    Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software