How does js delete the last element in an array
This article mainly introduces the relevant knowledge of "how js deletes the last element in an array". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to delete the last element of an array by js" can help you solve the problem.
1. Merge array: concat ()
Grammar
String.concat (string1, string2, … , stringX)
Example
Var arr = [1, 2, 3, 7]; arr.concat ([5, 5, 6, 7)) / / [1, 2, 3, 4, 4, 5, 6, 7] 2, adding elements at the end: push ()
Grammar
Array.push (item1, item2, … , itemX)
Example
Let a = [1JEI 2jue 3]; let item = a.push ('end'); / / 4console.log (a); / / [1JEI 2JI 3Jing']
3. Delete the last element in an array: pop ()
Grammar
Array.pop ()
Example
Var a = [1JI 2jue 3]; var b = a.pop () console.log (b) / / 3
4. Add one or more elements at the beginning: unshift ()
Grammar
Array.unshift (item1,item2, … , itemX)
Example
Let arr = [2, 3, 1, 9, 7, 5, 8, 22]; console.log (arr); arr.unshift ("new1", "new2", "new3"); console.log (arr); output: [2, 3, 1, 9, 7, 5, 8, 22] ['new1',' new2', 'new3', 2, 3, 1, 9, 7, 5, 8, 22]
5. Array sorting: sort ()
Grammar
Array.sort (sortfunction)
Example
Var a = [3, 1, 5, 5, 2]; var b = a.sort () console.log (b) / / [1, 2, 3, 4, 5]
6. Add / remove array elements: splice ()
Grammar
Array.splice (index,howmany,item1, … , itemX)
Example
Let arr = [2, 3, 1, 9, 7, 5, 8, 22]; console.log (arr); console.log (arr.splice (2,3, "this is a newly added element"); console.log (arr); output result: [2, 3, 1, 9, 7, 5, 8, 22] [1, 9, 7] [2, 3, 'this is a newly added element', 5, 8, 22]
7. Reverse the order of the elements in the array: reverse ()
Grammar
Array.reverse ()
Example
Let a = [1rem 2je 3]; a.reverse (); console.log (a); / / [3je 2jue 1] about "how js deletes the last element in an array", thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.