Home >Web Front-end >JS Tutorial >Detailed explanation of js array manipulation tutorial

Detailed explanation of js array manipulation tutorial

巴扎黑
巴扎黑Original
2017-07-18 15:42:201654browse

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]

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]

(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]

2: Deletion of array elements

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

 f1=[1,2,3,4,5,6

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 会该变原数组

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]

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

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!

Statement:
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