javascript
concat合并的数组会有顺序么_JS数组 Array
JS沒有真正數(shù)組,數(shù)組實際上是一種特殊的對象
創(chuàng)建數(shù)組的方法:
let arr = [1,2,3]; // let arr = new Array([1,2,3) let arr = new Array(2)偽數(shù)組:是在原型鏈中沒有數(shù)組的原型,也就是沒有數(shù)組共用的屬性的「數(shù)組」
let divlist = Document.querySelectorAll('div')合并數(shù)組
Array.prototype.concat() MDN
用于合并兩個或多個數(shù)組。此方法不會更改現(xiàn)有數(shù)組,而是返回一個新數(shù)組。
對應(yīng)字符串,同樣擁有String.prototype.concat()的方法合并新字符串。
let a = [1,2,3] let b = [2,4,6] let ab = a.concat(b) // (6) [1, 2, 3, 2, 4, 6]slice 截取數(shù)組
Array.prototype.slice()
語法:arr.slice( [begin [, end]] )
返回新的數(shù)組對象,由 begin 和 end 決定的原數(shù)組的淺拷貝,原始數(shù)組不會被改變。
let a = [1,2,3,4,5,6,7,8,9,10] let b = a.slice(5,10) // (5) [6, 7, 8, 9, 10]刪除數(shù)組
對象的增刪改查同樣可以運用在數(shù)組上,同時也會引發(fā)一些錯誤。修改后「數(shù)組的長度“l(fā)ength不會改變」
三種刪除數(shù)組元素的方法
Array.prototype.shift()
從數(shù)組中刪除第一個元素,并返回該元素的值。此方法更改數(shù)組的長度。
let a = [1,2,3,4,5,6,7,8,9,10] a.shift() // 不接受參數(shù),直接刪除引索為0的數(shù)值Array.prototype.pop()
從數(shù)組中刪除最后一個元素,并返回該元素的值。此方法更改數(shù)組的長度。
let a = [1,2,3,4,5,6,7,8,9,10] a.pop() // 不接受參數(shù),直接刪除引索為Array.prototype.splice()
刪除或替換現(xiàn)有元素或者原地添加新的元素來修改數(shù)組,并以數(shù)組形式返回被修改的內(nèi)容。此方法會改變原數(shù)組。
此方法同樣可以修改數(shù)組的值
語法:array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
let a = [1,2,3,4,5,6,7,8,9,10] // a.splice(5,1,666) // 從引索為 5 的數(shù)值開始,刪除 1 個數(shù)值,插入 666// 輸出結(jié)果:(10) [1, 2, 3, 4, 5, 666, 7, 8, 9, 10]添加數(shù)組
尾部添加元素
Array.push( item1 [,item2] )頭部添加元素
Array.unshift( item1 [,item2] )運行splice插入元素
Array.splise( index , 0 , 'x') // 在index位置插入‘x',0查看數(shù)組
IndexOf()
返回可以在數(shù)組中找到給定元素的第一個索引;如果不存在,則返回-1。
var array = [2, 9, 9]; array.indexOf(9); // 1 array.indexOf(7); // -1find()
返回提供的數(shù)組中滿足提供的測試功能的第一個元素的值。
let classScore = [{name: 'aziz', score: 100},{name: '吳彥祖', score: 96},{name: '學(xué)霸', score: 99} ];classScore.find( ({ name }) => name === 'aziz' ); // {name: "aziz", score: 100}findIndex()
返回滿足提供的測試功能的數(shù)組中第一個元素的索引 . 否則,它返回 -1
let classScore = [{name: 'aziz', score: 100},{name: '吳彥祖', score: 96},{name: '學(xué)霸', score: 99} ];classScore.findIndex( ({ name }) => name === 'aziz' ); // 0轉(zhuǎn)化數(shù)組
String.prototype.split()
let string = 'abc'; string.split('') // (3) ["1", "2", "3"] string.split(',') // ["123"]Array.from()
Array.from(string) // (3) ["1", "2", "3"]// 可以對數(shù)組進行map方法運算 Array.from([1, 2, 3], x => x + x) // (3) [2, 4, 6]遍歷數(shù)組
查看數(shù)字(字符串)的屬性名和值
let a = [1,2,3,4,5,6,7,8,9,10]; for(let i =0;i < a.length;i++){console.log(`${i}:${[i]}`) }forEach
Array.prototype.forEach()
語法:arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
let a = [1,2,3,4,5,6,7,8,9,10]; a.forEach(a => console.log(a));順序修改
反轉(zhuǎn)順序函數(shù)
Array.prototype.reverse()
用于修改數(shù)組順序,字符串需要 → 轉(zhuǎn)數(shù)組 → 轉(zhuǎn)換 → 合并
let s = 'abcde' s.split('').reverse().join('') //不改變原有值自定義順序
Array.prototype.sort MDN
對數(shù)組的元素進行排序,并返回數(shù)組。默認排序順序是在將元素轉(zhuǎn)換為字符串,然后比較它們的UTF-16代碼單元值序列時構(gòu)建的
var numbers = [4, 2, 5, 1, 3]; numbers.sort((a, b) => a - b); // a-b為默認順序,b-a相當(dāng)于“從大到小” // (5) [1, 2, 3, 4, 5]對象排序
??注意:會改變原有數(shù)組
let classScore = [{name:'aziz',score:78},{name:'胖虎', score:59},{name:'學(xué)霸',score:100} ]classScore.sort((a,b) => b.score - a.score)數(shù)組變換
對數(shù)組進行”運算“
Array.prototype.map()
map (對每一個數(shù)值進行操作)
let square = [1,2,3,4,5,6] square.map( item => item*item ) // (6) [1, 4, 9, 16, 25, 36]let abs = [1,2,3,4,5,6] abs.map(item => item*item) // (6) [1, 4, 9, 16, 25, 36]對數(shù)組進行”篩選“
Array.prototype.filter()
let a = [1,2,3,4,5,6,7,8,9,10] a.filter(a=> a%2===0) // (5) [2, 4, 6, 8, 10]對數(shù)組進行函數(shù)”運算“
Array.prototype.reduce()
屬于高級用法,基本可以實現(xiàn)大多數(shù)的數(shù)組運算。
有點類似for...if循環(huán),也可以結(jié)合if使用。
arr.reduce(callback( accumulator, currentValue)[ initialValue ]) -------------- 相當(dāng)于 -------------- arr.reduce( 累積器, 數(shù)組正在處理的元素 )[ 默認參數(shù)])實現(xiàn)數(shù)組值的相乘、相加
let a = [1,2,3,4,5,6,7,8,9,10]; a.reduce((acc,cur)=>{return acc*cur},1) // 3628800 a.reduce((acc,cur)=>{return acc+cur},0) // 55要注意,如果默認值為 0 ,則已 0 作為原始值計算
a.reduce((acc,cur)=>{return acc*cur},0) // 0高級用法,可以使用數(shù)組來統(tǒng)計名字,輸出結(jié)果成為一個對象
let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']let countedNames = names.reduce(function (allNames, name) { if (name in allNames) {allNames[name]++}else {allNames[name] = 1}return allNames }, {}) // countedNames is: // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }end.
JS Array運用 · 語雀
總結(jié)
以上是生活随笔為你收集整理的concat合并的数组会有顺序么_JS数组 Array的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vscode更改插件路径_vscode插
- 下一篇: springboot接口慢_【Sprin