Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js array manipulation tutorial

巴扎黑
Release: 2017-07-18 15:42:20
Original
1575 people have browsed it

1: Adding array elements

1: push

var f1=[1,2,3]
f1.push(4,5)
console.log(f1) //[1,2,3,4,5]

2: The unshift() method adds one or more elements to the beginning of the array and returns a new length.

var f1=[1,2,3]f1.unshift(4,5)
console.log(f1) //[4,5,1,2,3]
Copy after login

3:splice() method adds/removes items to/from the array, and then returns the deleted item

(1)Delete

var f1=[1,2,3,4,5,6,7]f1.splice(4,2)
console.log(f1) //[1, 2, 3, 4, 7]
Copy after login

(2) Delete and add

var f1=[1,2,3,4,5,6]
f1.splice(1,2,'h')
console.log(f1) //[1, "h", 4, 5, 6]
Copy after login

2: Deletion of array elements

1: pop(); //Remove the last An element and returns the element value

 f1=[1,2,3,4,5,6
Copy after login

2:shift(); //Remove the first element and return the element value, the elements in the array are automatically moved forward

var f1=[1,2,3,4,5,6]
console.log(f1.shift()) //1
console.log(f1) //[2, 3, 4, 5, 6]

3:splice(deletePos,deleteCount); //Delete the specified number of deleteCount elements starting from the specified position deletePos, and return the removed elements in array form

See the previous addition of array elements 3

3: Interception and merging of elements

1:slice(start, end); //Return the array in the form of an array part, please note that the element corresponding to end is not included. If end is omitted, all elements after start will be copied

var f1=[1,2,3,4,5,6]
console.log(f1.slice(3)) //[4, 5, 6]console.log(f1)  //[1,2, 3, 4, 5, 6] 不会改变数组   splice 会该变原数组
Copy after login

2: The concat() method is used to connect two or more arrays.

var f1=[1,2];var f2=[3,4]
console.log(f1.concat(f2)) //[1, 2, 3, 4]
Copy after login

Four: Copy of Array

1, slice(0); //Return the copy array of the array. Note that it is a new array, not a pointer to
2, concat(); //Return a copy array of the array, note that it is a new array, not a pointer to

5: Stringization of array elements

join(separator); / /Returns a string that concatenates each element value of the array, separated by a separator.

var f1=['apple','banner','orange']
console.log(f1.join()) //apple,banner,orange
Copy after login

The above is the detailed content of Detailed explanation of js array manipulation tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template