常用的陣列操作方法

2021-10-18 20:16:47 字數 4296 閱讀 7185

陣列新增:push(後面新增),unshift(前面新增)

let arr =

["1"

,"2"

,"3"];

console.

log(arr.

push(0

,4))

;// 新增後的陣列長度:5

console.

log(arr)

;//["1", "2", "3", 0, 4]

let arr =

["1"

,"2"

,"3"];

console.

log(arr.

unshift(0

,4))

;// 新增後的陣列長度:5

console.

log(arr)

;//[ 0, 4,"1", "2", "3"]

陣列刪除:pop(後面刪除),shift(前面刪除),只能刪除乙個

let arr =

["1"

,"2"

,"3"

,"4"

,"5"];

console.

log(arr.

pop())

;// 原陣列陣列長度:5

console.

log(arr)

;//["1", "2", "3", "4"]

let arr =

["1"

,"2"

,"3"

,"4"

,"5"];

console.

log(arr.

shift()

);// 原陣列陣列長度:5

console.

log(arr)

;//["2", "3", "4", "5"];

陣列刪除、替換、新增:splice(index,length,key)

index:陣列開始下標

length: 替換/刪除的長度

key:替換的值,預設為空

//刪除

let arr =

["a"

,"b"

,"c"];

console.

log(arr.

splice(1

,1))

;// 刪除的值:"b"

console.

log(arr)

;// ["a","c"]

//替換

let arr =

["a"

,"b"

,"c"];

console.

log(arr.

splice(1

,1,1

));// 替換的值:"b"

console.

log(arr)

;// ["a","1","c"]

//新增

let arr =

["a"

,"b"

,"c"];

console.

log(arr.

splice(1

,0,"你好"))

;// 空陣列

console.

log(arr)

;// ["a", "你好", "b", "c"]

陣列合併:concat(附:另一種方法)

//方法一

let arr =[1

,2,3

,4,5

];let arr1 = arr.

concat([

1,2]

);console.

log(arr)

;// [1,2,3,4,5]

console.

log(arr1)

;// [1,2,3,4,5,1,2]

//方法二

var arr1=

['a'

,'b'

,'c'

,'d'];

var arr2=

['d'];

var arr3=

['e'

,'f'];

var dd=

[...arr1,

...arr2,

...arr3]

;console.

log(dd)

;//["a", "b", "c", "d", "d", "e", "f"]

字串轉化為陣列:split

let str =

'12345'

;let arr = str.

split(''

);console.

log(arr)

;// ["1", "2", "3", "4", "5"]

陣列排序:sort

let arr =[2

,10,6

,1,4

,22,3

]let arr1 = arr.

sort

((a, b)

=>a - b)

console.

log(arr1)

// [1, 2, 3, 4, 6, 10, 22]

let arr2 = arr.

sort

((a, b)

=>b-a)

console.

log(arr2)

// [22, 10, 6, 4, 3, 2, 1]

陣列反**reverse

let arr =[1

,2,3

,4,5

]let arr1 = arr.

reverse()

console.

log(arr1)

// [5,4,3,2,1]

陣列去重:

var array =[1

,2,3

,3,4

,5];

//方法一if(

newset

(array)

.size !== array.length)

else

//方法二

let ary = array.

from

(new

set(array)

)console.

log(ary)

// [1, 2, 3, 4, 5]

以上方法不適合陣列物件或陣列包陣列的情況

陣列遍歷:foreach,map

//foreach

let arr =[1

,2,3

,4,5

]arr.

foreach

((value,index,array)

=>

index:

$array:$`

)})// value:1 index:0 array:1,2,3,4,5

// value:2 index:1 array:1,2,3,4,5

// value:3 index:2 array:1,2,3,4,5

// value:4 index:3 array:1,2,3,4,5

// value:5 index:4 array:1,2,3,4,5

//map

let arr =[1

,2,3

,4,5

];let info = arr.

map(

(value, index, array)

=>;}

);console.

log(info)

;

arr.foreach()是和for迴圈一樣,是代替for。arr.map()是修改陣列其中的資料,並返回新的資料。

arr.foreach() 沒有return; arr.map() 有return

陣列篩選:filter

let arr =[1

,2,3

,4,5

];let arr1 = arr.

filter

((value, index)

=>})

;console.

log(arr1)

;// [4, 5]

常用陣列的操作方法

tostring 把陣列轉換成乙個字串 var arr 1,2,3,4 console.log arr.tostring 1,2,3,4 join 把陣列轉換成乙個用符號連線的字串 var arr 1,2,3,4 console.log arr.join 1 2 3 4 concat 將兩個陣列合併...

JavaScript常用陣列操作方法

concat 方法用於連線兩個或多個陣列。該方法不會改變現有的陣列,僅會返回被連線陣列的乙個副本。var arr1 1,2,3 var arr2 4,5 var arr3 arr1.concat arr2 console.log arr1 1,2,3 console.log arr3 1,2,3,4...

js常用陣列操作方法

concat 方法用於連線兩個或多個陣列。該方法不會改變現有的陣列,僅會返回被連線陣列的乙個副本。var arr1 1,2,3 var arr2 4,5 var arr3 arr1.concat arr2 console.log arr1 1,2,3 console.log arr3 1,2,3,4...