当前位置:网站首页>JS array splicing "suggested collection"

JS array splicing "suggested collection"

2022-06-27 09:41:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

js Array splicing method of

Method 1 :concat Method splicing ( Returns a new array )

var a1 = [1,2,3,4,5];

var a2 = [‘a’,’b’,’c’];

var newa = a1.concat(a2);

Result Type :object

concat Method : Used to connect two or more arrays , Generate a new array ,concat The following array is the element of the array when operating , Not an array

Method 2 :join Method splicing ( Returns a new array )

var a1 = [1,2,3,4,5];

var a2 = [‘a’,’b’,’c’];

var newa = a1.join()+’,’+a2.join();

Return results :1,2,3,4,5,a,b,c

Result Type :string

join Method : Put all the elements of the array into a string , And then connect these strings , Insert... Between two elements separator String .separator stay join() Comma when no delimiter is added (‘,’), stay join(‘.’) When adding a delimiter, use the provided symbol to split .

Method 3 : combination apply perhaps call Use

var a1 = [1,2,3,4,5];

var a2 = [‘a’,’b’,’c’];

a1.push.apply(a1,a2);

Result Type :object

apply Method : Write methods for different objects , take a1 Of push Methods used in a1 On , receive a2( An array type ) Parameters

var a1 = [1,2,3,4,5];

var a2 = [‘a’,’b’,’c’];

a1.push.call(a1,…a2);

Result Type :object

call Method : A method that calls an object , Replace the current object with another object , The parameters of an object are parameter sequences

explain :

call([thisObj[,arg1[, arg2[, [,.argN]]]]])

Parameters

1)thisObj optional . The object that will be used as the current object .

2)arg1, arg2, , argN optional . Method parameter sequence to be passed .

Method four :es6 The extension operator of

var a1 = [1,2,3,4,5];

var a2 = [‘a’,’b’,’c’];

a1.push(…a2)

Result Type :object

Extension operator : like rest Inverse operation of parameter , Convert an array into a comma separated sequence of parameters , Mainly used for function calls .

Method five : Traversal array

var a1 = [1,2,3,4,5];

var a2 = [‘a’,’b’,’c’];

a2.forEach((item) => {a1.push(item); })

Result Type :object

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/133635.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270932392953.html