Common methods of arrays

Several methods commonly used in arrays

push: add an item to the end of the array and the return value is the new length of
the array unshift: add an item to the beginning of the array and the return value is the new length of the array
pop: delete the end of the array and the return value is the deleted array item
shift: delete the beginning of the array item returns the first item that was deleted
splice: delete any item in the array The return value is the deleted array item
slice: copy the array The return value is the new array copied to after writing the value does not contain the last item copied

Concatenation:
concat: concatenate an array with another array to return the concatenated array 
join: concatenate each item in the array into a string according to the specified delimiter

Sort:
reverse: reverse order array return value reverse order array original array change
sort: bubble sort according to anonymous function ba reverse order ab ascending order

Poor compatibility:
indexOf: returns the index of the item in the array
lastIndexOf: returns the last index of the item in the array
forEach: loops through the array parameter is an anonymous function and returns undefined by default

map: loop through the array parameter is an anonymous function


Extended use of splice:
Simulate push ary.splice(ary.length,0,x)
emulate pop ary.splice(ary.length-1,1)
emulate unshift ary.splice(0,0,x)
emulate shift ary.splice(0,1)
splice(0) delete from 0 to the end ==> delete all operations ==> return all array items ==> clone the array
 
   

--------------------------------------------------------------------------------------------------------

The delete method of the supplementary object: (delete)

var obj = { 0: "Background", 1: "TG", 2: "AS", 3: "Crawler", 4: "User", 8: "Esports", 9: "Penguin"};

delete obj[2]console.log(obj)// {0: "Background", 1: "TG", 3: "Crawler", 4: "User", 8: "E-sports", 9: "Penguin" }

var Employee = {
  age: 28,
  name: 'abc',
  designation: 'developer'
}

console.log(delete Employee.name);   // returns true
console.log(delete Employee.age);    // returns true

// 当试着删除一个不存在的属性时
// 同样会返回true
console.log(delete Employee.salary); // returns true

You can also use delete to delete an array

var arr = [1, 2, 3, 4, 5, 6, 7, 8]

delete arr[3];
console.log(arr)//[1, 2, 3, undefined × 1, 5, 6, 7, 8]

Delete with delete, the length of the array remains unchanged, here is still 8


--------------------------------------------------------------------------------------------------------





Mainly explain the array interception:

The methods of array interception mainly include slice and splice. In addition, when it comes to interception, I will also bring string interception here. The main methods of string interception are substr and substring.

slice

grammar:

arrayObject.slice(start,end)
parameter describe
start Required. Specifies where to start the selection. If negative, it specifies the position from the end of the array. That is, -1 refers to the last element, -2 refers to the second-to-last element, and so on.
end 可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。

开始的下标是从0开始的,start包含,end不包含

当只有start,没有指定end时,会从开始一直截取到结束

slice()返回的是一个新的数组

var arr=["George","John","Thomas"]

console.log(arr.slice(1)); //["John", "Thomas"]
console.log(arr); //["George", "John", "Thomas"]


②只有起始值,并且为负,默认的是从起始位置一直到整个数组结束,此处没有起始值,数组的最后一个下标是-1,比如此例,如果从-6开始取,必定是整个数组

var arr=[2,3,5,6,8,9]
var arr1=arr.slice(-1)
console.log(arr1) //[9] 只有起始参数,并且为负值,从数组尾部截取元素

④有起始值和结束值,并且为负,此处需注意下秒也是从小到大,同理,比如(-1,-6)也是肯定取不到值的,另外截取的下标个数也并不包含最后一个下标

var arr=[2,3,5,6,8,9]
var arr1=arr.slice(-6,-1)
console.log(arr1) //[2,3,5,6,8]

⑤有起始值和结束值,并且正负都有

负值到0

var arr=[2,3,5,6,8,9]
var arr1=arr.slice(-1,0)

console.log(arr1) //[ ] 从这里可以看出他的结果是为空的,所以从负值到0结果是取不到的


splice

定义和用法

splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。

注释:该方法会改变原始数组。

语法

arrayObject.splice(index,howmany,item1,.....,itemX)

参数 描述
index 必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。
howmany 必需。要删除的项目数量。如果设置为 0,则不会删除项目。
item1, ..., itemX 可选。向数组添加的新项目。

因为有第三个参数,可以通过splice方法在数组的指定位置添加数据,

例如 var arr=["George","John","Thomas"]

arr.splice(1,0,'hh')  

console.log(arr); ["George", "hh", "John", "Thomas"]



(3)substr 这个是字符串的用法 用法arr.substr(m,n) m必选也可以负值 n选填,注意看这个并不是结束为止而是截取的个数

示例如下:

只有一个参数 默认从此下标开始,取后面所有的

正值

var arr="sdhgfhf"
var arr1=arr.substr(1)
console.log(arr)// "sdhgfhf"这个是不变的,下面不再列举
console.log(arr1)// "dhgfhf" 从下标1开始取后面所有的 

负值

var arr="sdhgfhf"
var arr1=arr.substr(-2)
console.log(arr1)// "hf" 从下标-2开始取后面所有的 

两个参数,从第一个下标开始,截取到下标2的长度

var arr="sdhgfhf"
var arr1=arr.substr(1,3)
console.log(arr1)// "dhg" 从下标1开始取后面所有的 同理如果为负值,比如(-6,3)则为"hjf";后面的始终为长度

总结:此方法需注意不要和数组的混淆,这个是截取长度的

(4)substring  用法arr.substring(m,n) 两个参数,m必须,起始位置 ,n选填,截取下标的结束位置,此用法和上面slice有点类似,但是需要注意的是此方法是不支持负值的

示例如下:

只有一个参数

var arr="sdhgfhf"
var arr1=arr.substring(1)
console.log(arr1)//"dhgfhf"从下标1开始取后面所有的

两个参数

var arr="sdhgfhf"
var arr1=arr.substring(1,3)
console.log(arr1)// "dh"从下标1开始截取到下标3,但是不包含3

总结:此方法不支持负值,只针对下标来截取的

最后,以上主要是总结的数组的一些简单的操作方法,学无止境,继续努力。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325585017&siteId=291194637