在開發中,數組的使用場景很是多,平日中也涉及到不少數組相關操做,對一些常見的操做方法進行總結和收藏,在開發中就能信手拈來,大大提升開發效率。javascript
本文在gitthub作了收錄 github.com/Michael-lzg…vue
一、生成隨機數java
遍歷數組,每次循環都隨機一個在數組長度範圍內的數,並交換本次循環的位置和隨機數位置上的元素node
function randomSort1(arr) { for (let i = 0, l = arr.length; i < l; i++) { let rc = parseInt(Math.random() * l) // 讓當前循環的數組元素和隨機出來的數組元素交換位置 const empty = arr[i] arr[i] = arr[rc] arr[rc] = empty } return arr } var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] // 下面兩次的結果確定是不同的; console.log(randomSort1(arr1)) console.log(randomSort1(arr1))
二、生成新數組webpack
function randomSort2(arr) { var mixedArr = [] while (arr.length > 0) { let rc = parseInt(Math.random() * arr.length) mixedArr.push(arr[rc]) arr.splice(rc, 1) } return mixedArr } // 例子 var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] console.log(randomSort2(arr1))
三、 arr.sortgit
function randomSort3(arr) { arr.sort(function (a, b) { return Math.random() - 0.5 }) return arr } // 例子 var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] console.log(randomSort3(arr1))
一、單個屬性排序github
function compare(property) { return function (a, b) { let value1 = a[property] let value2 = b[property] return value1 - value2 } } let arr = [ { name: 'zopp', age: 10 }, { name: 'gpp', age: 18 }, { name: 'yjj', age: 8 }, ] console.log(arr.sort(compare('age')))
二、多個屬性排序web
function by(name, minor) { return function(o, p) { let a, b if (o && p && typeof o === 'object' && typeof p === 'object') { a = o[name] b = p[name] if (a === b) { return typeof minor === 'function' ? minor(o, p) : 0 } if (typeof a === typeof b) { return a < b ? -1 : 1 } return typeof a < typeof b ? -1 : 1 } else { thro('error') } } },
一、調用 ES6 中的 flat 方法vue-cli
ary = arr.flat(Infinity) console.log([1, [2, 3, [4, 5, [6, 7]]]].flat(Infinity))
二、普通遞歸npm
let result = [] let flatten = function (arr) { for (let i = 0; i < arr.length; i++) { let item = arr[i] if (Array.isArray(arr[i])) { flatten(item) } else { result.push(item) } } return result } let arr = [1, 2, [3, 4], [5, [6, 7]]] console.log(flatten(arr))
三、利用 reduce 函數迭代
function flatten(arr) { return arr.reduce((pre, cur) => { return pre.concat(Array.isArray(cur) ? flatten(cur) : cur) }, []) } let arr = [1, 2, [3, 4], [5, [6, 7]]] console.log(flatten(arr))
四、擴展運算符
function flatten(arr) { while (arr.some((item) => Array.isArray(item))) { arr = [].concat(...arr) } return arr } let arr = [1, 2, [3, 4], [5, [6, 7]]] console.log(flatten(arr))
一、利用數組的 indexOf 下標屬性來查詢
function unique(arr) { var newArr = [] for (var i = 0; i < arr.length; i++) { if (newArr.indexOf(arr[i]) === -1) { newArr.push(arr[i]) } } return newArr } console.log(unique([1, 1, 2, 3, 5, 3, 1, 5, 6, 7, 4]))
二、先將原數組排序,在與相鄰的進行比較,若是不一樣則存入新數組。
function unique(arr) { var formArr = arr.sort() var newArr = [formArr[0]] for (let i = 1; i < formArr.length; i++) { if (formArr[i] !== formArr[i - 1]) { newArr.push(formArr[i]) } } return newArr } console.log(unique([1, 1, 2, 3, 5, 3, 1, 5, 6, 7, 4]))
三、利用對象屬性存在的特性,若是沒有該屬性則存入新數組。
function unique(arr) { var obj = {} var newArr = [] for (let i = 0; i < arr.length; i++) { if (!obj[arr[i]]) { obj[arr[i]] = 1 newArr.push(arr[i]) } } return newArr } console.log(unique([1, 1, 2, 3, 5, 3, 1, 5, 6, 7, 4]))
四、利用數組原型對象上的 includes 方法。
function unique(arr) { var newArr = [] for (var i = 0; i < arr.length; i++) { if (!newArr.includes(arr[i])) { newArr.push(arr[i]) } } return newArr } console.log(unique([1, 1, 2, 3, 5, 3, 1, 5, 6, 7, 4]))
五、利用數組原型對象上的 filter 和 includes 方法。
function unique(arr) { var newArr = [] newArr = arr.filter(function (item) { return newArr.includes(item) ? '' : newArr.push(item) }) return newArr } console.log(unique([1, 1, 2, 3, 5, 3, 1, 5, 6, 7, 4]))
六、利用 ES6 的 set 方法。
function unique(arr) { return Array.from(new Set(arr)) // 利用Array.from將Set結構轉換成數組 } console.log(unique([1, 1, 2, 3, 5, 3, 1, 5, 6, 7, 4]))
方法一
function unique(arr) { const res = new Map() return arr.filter((item) => !res.has(item.productName) && res.set(item.productName, 1)) }
方法二
function unique(arr) { let result = {} let obj = {} for (var i = 0; i < arr.length; i++) { if (!obj[arr[i].key]) { result.push(arr[i]) obj[arr[i].key] = true } } }
一、includes 方法結合 filter 方法
let a = [1, 2, 3] let b = [2, 4, 5] // 並集 let union = a.concat(b.filter((v) => !a.includes(v))) // [1,2,3,4,5] // 交集 let intersection = a.filter((v) => b.includes(v)) // [2] // 差集 let difference = a.concat(b).filter((v) => !a.includes(v) || !b.includes(v)) // [1,3,4,5]
二、ES6 的 Set 數據結構
let a = new Set([1, 2, 3]) let b = new Set([2, 4, 5]) // 並集 let union = new Set([...a, ...b]) // Set {1, 2, 3, 4,5} // 交集 let intersect = new Set([...a].filter((x) => b.has(x))) // set {2} // 差集 let difference = new Set([...a].filter((x) => !b.has(x))) // Set {1, 3, 4, 5}
一、萬能的 for 循環
function sum(arr) { var s = 0 for (var i = arr.length - 1; i >= 0; i--) { s += arr[i] } return s } sum([1, 2, 3, 4, 5]) // 15
二、遞歸方法
function sum(arr) { var len = arr.length if (len == 0) { return 0 } else if (len == 1) { return arr[0] } else { return arr[0] + sum(arr.slice(1)) } } sum([1, 2, 3, 4, 5]) // 15
三、ES6 的 reduce 方法
function sum(arr) { return arr.reduce(function (prev, curr) { return prev + curr }, 0) } sum([1, 2, 3, 4, 5]) // 15
一、Array 的 slice 方法
let arr = Array.prototype.slice.call(arguments)
二、ES6 的 Array.from()
let arr = Array.from(arguments)
三、擴展運算符...
let arr = [...arguments]
function swapItems(arr, index1, index2) { arr[index1] = arr.splice(index2, 1, arr[index1])[0] return arr } function up(arr, index) { if (index === 0) { return } this.swapItems(arr, index, index - 1) } function down(arr, index) { if (index === this.list.length - 1) { return } this.swapItems(arr, index, index + 1) }
將以下數據轉化爲樹狀結構
let arr = [ { id: 1, name: '1', pid: 0, }, { id: 2, name: '1-1', pid: 1, }, { id: 3, name: '1-1-1', pid: 2, }, { id: 4, name: '1-2', pid: 1, }, { id: 5, name: '1-2-2', pid: 4, }, { id: 6, name: '1-1-1-1', pid: 3, }, { id: 7, name: '2', }, ]
實現方法
function toTree(data, parentId = 0) { var itemArr = [] for (var i = 0; i < data.length; i++) { var node = data[i] if (node.pid === parentId) { var newNode = { ...node, name: node.name, id: node.id, children: toTree(data, node.id), } itemArr.push(newNode) } } return itemArr } console.log(toTree(arr))
總結18個webpack插件,總會有你想要的!
搭建一個 vue-cli4+webpack 移動端框架(開箱即用)
從零構建到優化一個相似vue-cli的腳手架
封裝一個toast和dialog組件併發布到npm
從零開始構建一個webpack項目
總結幾個webpack打包優化的方法
總結vue知識體系之高級應用篇
總結vue知識體系之實用技巧
總結vue知識體系之基礎入門篇
總結移動端H5開發經常使用技巧(乾貨滿滿哦!)