前言

我是歌谣 放弃很容易 但是坚持一定很酷 微信公众号关注前端小歌谣带你进入前端巅峰交流群 今天继续对前端知识的小结

数组去重的方式

<!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>数组去重的第二种方式</title> </head> <body> <div id="geyao"></div> </body> <script> var geyao = [1, 2, 3, 4, 6, 7, 1, 2, 3, 8, 9] function ArrayCommon(arr) { //判断是不是数组 不是数组就返回一个false if (!Array.isArray(arr)) { console.log('这不是一个数组哦') return } //元素在数组中第一次出现的位置是否和元素现在的位置相等 return Array.prototype.filter.call(arr,function(item,index){ console.log(arr.indexOf(item),item,index) return arr.indexOf(item)===index }) } console.log(ArrayCommon(geyao, 'geyao')) // document.getElementById("geyao").innerText=ArrayCommon(geyao) </script> </html>

数组扁平化

<!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>数组扁平化第一种方式 循环</title> </head> <body> <script> let arr = [0, [1, [2, 3, [4, [5, 6]], 7]], [8, 9]] function geyao(arr) { var show = [] for (var i = 0; i < arr.length; i++) { //如果arr[i]是一个数组 if (Array.isArray(arr[i])) { //递归遍历arr[i] show.push.apply(show, geyao(arr[i])) } else { show.push(arr[i]) } } return show } console.log(geyao(arr)) //[0,1,2,3,4,5,6,7,8,9] </script> </body> </html>

数组扁平化reduce方法

```


reduce方法

数组方法splice

<!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>数组方法splice</title> </head> <body> <script> //splice(start,end,index1,index2) 从什么位置开始,删几个?删了后插入什么新元素? var geyao=["1","2","3","4"] var geyaotest=["1","2","3","4"] var geyaofangfang=["1","2","3","4"] var fangfang=["1","2","3","4"] var fangfangTest=["1","2","3","4"] var fangfangBang=["1","2","3","4"] var kangkang=["1","2","3","4"] var kangkangni=["1","2","3","4"] var kangkanghao=["1","2","3","4"] //1个参数会截取当前 大于2之后的数都会被删除 geyao.splice(2) console.log(geyao,"geyao")//["1","2"] //2个参数 表示从什么位置截取到什么位置 把下标为2的截取掉 geyaotest.splice(2,1) console.log(geyaotest,"geyaotest") //[ "1", "2", "4" ] //3个参数 在什么位置到什么位置添加 geyaofangfang.splice(2,1,"5") console.log(geyaofangfang,"geyaofangfang") //[ "1", "2", "5", "4" ] //超出数组长度 默认获得前面的全部 fangfang.splice(5) console.log(fangfang,"fangfang") // [ "1", "2", "3", "4" ] //从5之后开始截取 fangfangTest.splice(5,1) console.log(fangfangTest,"fangfangTest") // [ "1", "2", "3", "4" ] //对后置的位置进行添加 fangfangBang.splice(5,1,"6") console.log(fangfangBang,"fangfangBang") // [ "1", "2", "3", "4","6"] //倒着开始截取剩余部分 kangkang.splice(-1) console.log(kangkang,"kangkang") // [ "1", "2", "3" ] //倒着进行处理 kangkangni.splice(-2,1) console.log(kangkangni,"kangkangni")//[ "1", "2", "4" ] //倒着插入对应值 kangkanghao.splice(-2,1,"8") console.log(kangkanghao,"kangkanghao")//[ "1", "2", "8", "4" ] //该方法向或者从数组中添加或者删除项目,返回被删除的项目。(该方法会改变原数组) </script> </body> </html>

数组特性

<!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>数组特性</title> </head> <body> <script> // 数组空位 // 数组可以使用一串逗号来创建空位,逗号前面为一项,值为undefined // 改变length会直接作用于原数组: // 减小length会从数组尾部 删除元素 // 增大length会向数组尾部添加值为undefined的元素 </script> </body> </html>

总结

我是歌谣 最好的种树是十年前 其次是现在 加油 歌谣