1. 首页
  2. IT资讯

11种数组去重方法,值得你收藏!

“u003Cdivu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fdfic-imagehandleru002F9c027ef9-6ddf-45ba-87a8-f7646b5c6564″ img_width=”280″ img_height=”165″ alt=”11种数组去重方法,值得你收藏!” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpu003EQuestion : 现在有一个字符数组 :u003Cu002Fpu003Eu003Cpreu003Elet arr = [“盆”,”盆”,”学”,”I”,”T”,”越”,”学”,”越”,”牛”,”B”,”!”];u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E你有几种方法去除数组中重复的内容呢?u003Cu002Fpu003Eu003Ch1u003E方法一 ES6 Set去重u003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E return Array.from(new Set(arr))u003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F[ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法二 for循环+ splice去重u003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E for (let i = 0; i < arr.length; i++) {u003Cbru003E for (let j = i + 1; j < arr.length; j++) {u003Cbru003E if (arr[j] === arr[i]) {u003Cbru003E arr.splice(j, 1);u003Cbru003E j–;u003Cbru003E }u003Cbru003E }u003Cbru003E }u003Cbru003E return arru003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E想想:如果我写成如下的形式,会有什么问题?u003Cu002Fpu003Eu003Cpreu003Efunction uniqueArray(arr) {u003Cbru003E let len = arr.length;u003Cbru003E for (let i = 0; i < len; i++) {u003Cbru003E for (let j = i + 1; j < len; j++) {u003Cbru003E if (arr[j] === arr[i]) {u003Cbru003E arr.splice(j, 1);u003Cbru003E j–;u003Cbru003E }u003Cbru003E }u003Cbru003E }u003Cbru003E return arru003Cbru003E}u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法三 indexOf 去重u003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E if (!Array.isArray(arr)) {u003Cbru003E console.log(‘type error!’);u003Cbru003E return;u003Cbru003E }u003Cbru003E var array = [];u003Cbru003E for (var i = 0; i < arr.length; i++) {u003Cbru003E if (array.indexOf(arr[i]) === -1) {u003Cbru003E array.push(arr[i])u003Cbru003E }u003Cbru003E }u003Cbru003E return array;u003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E方法四 利用sort()函数去重u003Cu002Fpu003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Eu002Fu002F 利用sort()排序方法,然后根据排序后的结果进行遍历及相邻元素比对u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E if (!Array.isArray(arr)) {u003Cbru003E console.log(‘type error!’)u003Cbru003E return;u003Cbru003E }u003Cbru003E arr = arr.sort()u003Cbru003E var arrry= [arr[0]];u003Cbru003E for (var i = 1; i < arr.length; i++) {u003Cbru003E if (arr[i] !== arr[i-1]) {u003Cbru003E arrry.push(arr[i]);u003Cbru003E }u003Cbru003E }u003Cbru003E return arrry;u003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法五 利用includesu003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E var array =[];u003Cbru003E u002Fu002Fincludes 检测数组是否有某个值u003Cbru003E for(var i = 0; i < arr.length; i++) {u003Cbru003E if( !array.includes( arr[i]) ) {u003Cbru003E array.push(arr[i]);u003Cbru003E }u003Cbru003E }u003Cbru003E return arrayu003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法六 利用 filter + hasOwnPropertyu003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E var obj = {};u003Cbru003E return arr.filter(function(item){u003Cbru003E return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)u003Cbru003E })u003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法七 利用 filter + indexOfu003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E return arr.filter(function(item, index, arr) {u003Cbru003E u002Fu002F当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素u003Cbru003E return arr.indexOf(item,0) === index;u003Cbru003E });u003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法八 递归法u003Cu002Fh1u003Eu003Cpu003Elet arr = [“盆”, “盆”, “学”, “盆”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cu002Fpu003Eu003Cpu003Efunction uniqueArray(arr) {u003Cu002Fpu003Eu003Cpu003E let array = arr;u003Cu002Fpu003Eu003Cpu003E let len = array.length;u003Cu002Fpu003Eu003Cpu003E array.sort();u002Fu002F首先排序,相同元素在一起,方便后续递归去重u003Cu002Fpu003Eu003Cpu003E function loop(index) {u003Cu002Fpu003Eu003Cpu003E if (index >= 1) {u003Cu002Fpu003Eu003Cpu003E if (array[index] === array[index – 1]) {u003Cu002Fpu003Eu003Cpu003E array.splice(index, 1);u003Cu002Fpu003Eu003Cpu003E }u003Cu002Fpu003Eu003Cpu003E loop(index – 1); u002Fu002F递归loopu003Cu002Fpu003Eu003Cpu003E }u003Cu002Fpu003Eu003Cpu003E }u003Cu002Fpu003Eu003Cpu003E loop(len – 1);u003Cu002Fpu003Eu003Cpu003E return array;u003Cu002Fpu003Eu003Cpu003E}u003Cu002Fpu003Eu003Cpu003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘!’, ‘B’, ‘I’, ‘T’, ‘学’, ‘牛’, ‘盆’, ‘越’ ]u003Cu002Fpu003Eu003Ch1u003E方法九 Map数据结构去重u003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “盆”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E let map = new Map();u003Cbru003E let array = [] u002Fu002F 数组用于返回结果u003Cbru003E for (let i = 0; i < arr.length; i++) {u003Cbru003E if (map.has(arr[i])) { u002Fu002F 如果有该key值u003Cbru003E map.set(arr[i], true);u003Cbru003E } else {u003Cbru003E map.set(arr[i], false); u002Fu002F 如果没有该key值u003Cbru003E array.push(arr[i]);u003Cbru003E }u003Cbru003E }u003Cbru003E return array;u003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法十 reduce+includesu003Cu002Fh1u003Eu003Cpreu003Elet arr = [“盆”, “盆”, “学”, “盆”, “I”, “T”, “越”, “学”, “越”, “牛”, “B”, “!”];u003Cbru003Efunction uniqueArray(arr) {u003Cbru003E return arr.reduce((prev,cur) => prev.includes(cur) ? prev : […prev,cur]);u003Cbru003E}u003Cbru003Econsole.log(uniqueArray(arr)); u002Fu002F [ ‘盆’, ‘学’, ‘I’, ‘T’, ‘越’, ‘牛’, ‘B’, ‘!’ ]u003Cbru003Eu003Cu002Fpreu003Eu003Ch1u003E方法十一 懒人法u003Cu002Fh1u003Eu003Cpreu003E[…new Set(arr)] u002Fu002F 对于不想多写代码的人来说,这么一句就够了!u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E这么多种方法,你都学会了吗?u003Cu002Fpu003Eu003Cpu003E你还有什么更好的方法,欢迎留言讨论吧!u003Cu002Fpu003Eu003Cu002Fdivu003E”

原文始发于:11种数组去重方法,值得你收藏!

主题测试文章,只做测试使用。发布者:℅傍ㄖ免沦陷dε鬼,转转请注明出处:http://www.cxybcw.com/11254.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code