js操作数组的多种方法 shift()、unshift()、push()、pop()、concat()、slice()、splice()、toString()、join()

1.shift() :把数组的第一个元素从其中删除,并返回第一个元素的值。该方法会改变原数组。

const array=['one','two','three','four'];
const shiftResult=array.shift();
console.log(shiftResult); //one
console.log(array);  // ['two','three','four']

 

2.unshift():向数组的开头添加一个或更多元素,并返回新数组的长度。该方法会改变原数组。

const array=['one','two','three','four'];
const unshiftResult = array.unshift('new1','new2');
console.log(unshiftResult);//6
console.log(array); //['new1','new2','one','two','three','four']

 

3.push():向数组的末尾添加一个或多个元素,并返回新的长度。该方法会改变原数组。

const array=['one','two','three','four'];
const pushResult = array.push('new1','new2');
console.log(pushResult);// 6
console.log(array); //['one','two','three','four','new1','new2']

4.pop():把数组的最后一个元素从其中删除,并返回最后一个元素的值。该方法会改变原数组。

const array=['one','two','three','four'];
const popResult = array.pop();
console.log(popResult);// four
console.log(array);// ['one','two','three']

5.concat():用于连接两个或多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。

const array=['one','two','three','four'];
const arrar1=['new3','new4'];
const arrar2 = ['new5', 'new6'];
const concatResult1 = array.concat('new1','new2');
const concatResult2 = array.concat(arrar1);
const concatResult3 = array.concat(arrar1, arrar2);
console.log(concatResult1);//['one','two','three','four','new1','new2']
console.log(concatResult2);//['one','two','three','four','new3','new4']
console.log(concatResult3);//['one','two','three','four','new1','new2','new3','new4']
console.log(array);//['one','two','three','four']

6.slice(start,end):截取数组的某一部分,该方法并不会修改数组,而是返回一个子数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。
 

 
参数参数描述
start必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。
end可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。
const array=['one','two','three','four','five','six','seven'];
const sliceResult1 = array.slice(-4,-2);
const sliceResult2 = array.slice(-2);
const sliceResult3 = array.slice(2,6);
const sliceResult4 = array.slice(2);
console.log(sliceResult1); //['four','five'];
console.log(sliceResult2);//['six','seven'];
console.log(sliceResult3);//['three','four','five','six'];
console.log(sliceResult4);//['three','four','five','six','seven'];
console.log(array);//['one','two','three','four','five','six','seven'];

7.splice(index,howmany,item1, ..., itemX): 向/从数组中添加/删除项目,然后返回被删除的数组。该方法会改变原始数组。

参数参数描述
index必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。
howmany必需。要删除的项目数量。如果设置为 0,则不会删除项目。
item1, ..., itemX可选。向数组添加的新项目。
//向原数组某个位置添加一个元素
const array=['one','two','three','four'];
array.splice(2,0,'new1');
console.log('array:', array);//['one','two','three','new1','four']
//删除位于 index 2 的元素,并添加一个新元素来替代被删除的元素
 const array=['one','two','three','four'];
 array.splice(2,1,'new1');
 console.log('array:', array);//['one','two',new1','four']
//删除从 index 2  开始的三个元素,并添加一个新元素来替代被删除的元素
 const array=['one','two','three','four','five','six','seven'];
 array.splice(2,3,'new1');
 console.log('array:', array);//['one','two','new1','six','seven']

8.toString():可把一个逻辑值转换为字符串,并返回结果。

const array=['one','two','three','four'];
const toStringResult = array.toString();
console.log('array:', toStringResult);//'one,two,three,four'

9.join(separator):把数组中的所有元素放入一个字符串。

参数参数描述
separator可选。指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。
const array=['one','two','three','four'];
const joinResult = array.join();
const joinResult1 = array.join('/');
console.log(joinResult);//"one,two,three,four"
console.log(joinResult1);//"one/two/three/four"

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值