<legend id='4lcoP'><style id='4lcoP'><dir id='4lcoP'><q id='4lcoP'></q></dir></style></legend>

          <bdo id='4lcoP'></bdo><ul id='4lcoP'></ul>
      1. <small id='4lcoP'></small><noframes id='4lcoP'>

        <tfoot id='4lcoP'></tfoot>
      2. <i id='4lcoP'><tr id='4lcoP'><dt id='4lcoP'><q id='4lcoP'><span id='4lcoP'><b id='4lcoP'><form id='4lcoP'><ins id='4lcoP'></ins><ul id='4lcoP'></ul><sub id='4lcoP'></sub></form><legend id='4lcoP'></legend><bdo id='4lcoP'><pre id='4lcoP'><center id='4lcoP'></center></pre></bdo></b><th id='4lcoP'></th></span></q></dt></tr></i><div id='4lcoP'><tfoot id='4lcoP'></tfoot><dl id='4lcoP'><fieldset id='4lcoP'></fieldset></dl></div>
      3. JavaScrip数组去重操作实例小结

        本文将详细讲解“JavaScript 数组去重操作实例小结”,包括去重的常用方法以及实例说明。
        <tfoot id='gMsfz'></tfoot>
      4. <i id='gMsfz'><tr id='gMsfz'><dt id='gMsfz'><q id='gMsfz'><span id='gMsfz'><b id='gMsfz'><form id='gMsfz'><ins id='gMsfz'></ins><ul id='gMsfz'></ul><sub id='gMsfz'></sub></form><legend id='gMsfz'></legend><bdo id='gMsfz'><pre id='gMsfz'><center id='gMsfz'></center></pre></bdo></b><th id='gMsfz'></th></span></q></dt></tr></i><div id='gMsfz'><tfoot id='gMsfz'></tfoot><dl id='gMsfz'><fieldset id='gMsfz'></fieldset></dl></div>

        <small id='gMsfz'></small><noframes id='gMsfz'>

        1. <legend id='gMsfz'><style id='gMsfz'><dir id='gMsfz'><q id='gMsfz'></q></dir></style></legend>
                  <bdo id='gMsfz'></bdo><ul id='gMsfz'></ul>
                    <tbody id='gMsfz'></tbody>

                  本文将详细讲解“JavaScript 数组去重操作实例小结”,包括去重的常用方法以及实例说明。

                  一、常用去重方法

                  1. Set(ES6新增)

                  ES6 中引入了 Set 数据结构,它类似于数组,但是数组中的元素是不能重复的,可以很方便地实现数组去重。

                  const arr = [1, 2, 2, 3, 3, 4];
                  const uniqueArr = [...new Set(arr)];
                  console.log(uniqueArr); // [1, 2, 3, 4]
                  

                  2. filter

                  通过 Array.prototype.filter() 方法可以实现数组去重操作,将数组中的每个元素与其它元素比较,留下不重复的。

                  const arr = [1, 2, 2, 3, 3, 4];
                  
                  const uniqueArr = arr.filter((item, index) => {
                    return arr.indexOf(item) === index;
                  });
                  
                  console.log(uniqueArr); // [1, 2, 3, 4]
                  

                  3. reduce

                  使用 reduce 方法,遍历数组,将元素添加到结果数组中,如果该元素已存在于结果数组中,则不再添加。

                  const arr = [1, 2, 2, 3, 3, 4];
                  
                  const uniqueArr = arr.reduce((tempArr, cur) => {
                    if (!tempArr.includes(cur)) {
                      tempArr.push(cur);
                    }
                    return tempArr;
                  }, []);
                  
                  console.log(uniqueArr); // [1, 2, 3, 4]
                  

                  二、去重实例说明

                  1. 数组对象去重

                  考虑以下数组对象:

                  const userArr = [
                    { name: 'Alice', age: 18 },
                    { name: 'Bob', age: 20 },
                    { name: 'Alice', age: 18 }
                  ];
                  

                  我们可以使用 Set 去重:

                  const uniqueArr = [...new Set(userArr.map(user => JSON.stringify(user)))].map(str => JSON.parse(str));
                  console.log(uniqueArr); // [{name: "Alice", age: 18}, {name: "Bob", age: 20}]
                  

                  其中,数组对象首先需要转换成 JSON 字符串,然后再去重操作完成后再将其转换回数组对象。

                  2. 多维数组去重

                  对于多维数组,也需要用到上文提到的方法进行去重。如下所示:

                  const multiArr = [[1, 2, 3], [2, 3, 4], [1, 2]];
                  const uniqueArr = multiArr.reduce((tempArr, arr) => {
                    const str = arr.join();
                    if (!tempArr.includes(str)) {
                      tempArr.push(str);
                    }
                    return tempArr;
                  }, []).map(str => str.split(',').map(Number));
                  
                  console.log(uniqueArr); // [[1, 2, 3], [2, 3, 4], [1, 2]]
                  

                  其中,多维数组将每个子数组都转换成以逗号分隔的字符串,然后进行去重操作,最后在还原成多维数组。

                  以上就是本文的全部内容,希望对你有所帮助。

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Lambda表达式是Java 8中引入的新特性之一,它是一个匿名函数,可以捕获参数并表现为一个代码块,而不像方法一样需要一个固定的名称。它主要用于传递行为或代码块以及事件处理等操作。
                  下面为您详细讲解基于Java的回调函数。
                  在Java中,equals()是用来比较两个对象是否相等的函数。equals()方法是Object类中的方法,因此所有Java类都包含equals()方法。在默认情况下,equals()方法比较对象的引用地址是否相同,即两个对象是否是同一个实例。但是,我们可以覆盖equals()方法,来定义自
                  JavaWeb是Java在Web领域的应用,是目前非常热门的技术之一。但是JavaWeb涉及到的技术非常广泛,初学者很容易迷失方向。本文总结了JavaWeb的基础知识,为初学者提供了一份学习笔记分享,希望能够帮助大家快速入门。
                  在Java编程中,字符串操作是很常见的,而替换字符串是其中常用的操作之一。Java提供了三种函数用于替换字符串:replace、replaceAll和replaceFirst。这篇文章将为您详细介绍它们的用法。
                  进制是数学中一种表示数值大小的方法,常见的进制有10进制、2进制、16进制等。

                    <legend id='kuHw9'><style id='kuHw9'><dir id='kuHw9'><q id='kuHw9'></q></dir></style></legend>

                      • <bdo id='kuHw9'></bdo><ul id='kuHw9'></ul>
                            <tbody id='kuHw9'></tbody>
                            <tfoot id='kuHw9'></tfoot>

                            <small id='kuHw9'></small><noframes id='kuHw9'>

                          1. <i id='kuHw9'><tr id='kuHw9'><dt id='kuHw9'><q id='kuHw9'><span id='kuHw9'><b id='kuHw9'><form id='kuHw9'><ins id='kuHw9'></ins><ul id='kuHw9'></ul><sub id='kuHw9'></sub></form><legend id='kuHw9'></legend><bdo id='kuHw9'><pre id='kuHw9'><center id='kuHw9'></center></pre></bdo></b><th id='kuHw9'></th></span></q></dt></tr></i><div id='kuHw9'><tfoot id='kuHw9'></tfoot><dl id='kuHw9'><fieldset id='kuHw9'></fieldset></dl></div>