数组常用方法(2).html

<script type="text/javascript">
var arr=['hello','world','sql','linux']

//5.concat() 对数组进行拼接 可拼接多个数组
//语法:数组.concat(拼接的内容)
//不改变原始数组
//数组添加在末尾
//返回值:原数组+拼接后的数组
var res=arr.concat(['新来的1','新来的2'],['新来的11','新来的22'])
console.log(arr) //(4) ["hello", "world", "sql", "linux"]
console.log(res) //(6) ["hello", "world", "sql", "linux", "新来的1", "新来的2","新来的11", "新来的22"]

//6.splice() 对数组进行截取
//语法: 数组.splice(开始截取的索引, 截取多少个)
//直接改变原始数组
//返回值:以数组的形式返回已截取的内容,截取的内容为数组的形式返回

//splice使用方法1
// var res=arr.splice(1,2)
// console.log(arr) //(2) ["hello", "linux"]
// console.log(res) //(2) ["world", "sql"]

// var res=arr.splice(1,0)
// console.log(arr) //(4) ["hello", "world", "sql", "linux"]
// console.log(res) //[]

//splice使用方法2
//当第二个参数不传递时,从哪个索引开始截取到数组的末尾的值 当返回值
// var res=arr.splice(1, )
// console.log(arr) //["hello"]
// console.log(res) //(3) ["world", "sql", "linux"]

//splice使用方法3
//从第三个参数开始,都是替换掉截取的位置
// var res=arr.splice(1, 2,'新来的0','新来的1')
// console.log(arr) //(4) ["hello", "新来的0", "新来的1", "linux"]
// console.log(res) //(2) ["world", "sql"]

//插入功能
//按顺序排列
var arr1=[1,2,3,5,6,7,8,9]
var num=4
//从哪个索引位置开始删除,就从哪个索引位置开始替换
arr1.splice(3, 0, num)
console.log(arr1) //(9) [1, 2, 3, 4, 5, 6, 7, 8, 9]

//7.reverse() 反转数组
//语法:数组.reverse()
//直接改变原始数组
//返回值:反转后的数组 与arr同等
// var res=arr.reverse()
// console.log(arr) //(4) ["linux", "sql", "world", "hello"]
// console.log(res) //(4) ["linux", "sql", "world", "hello"]

var arr2=[1,2,3,4,5]
var res=arr2.reverse()
console.log(arr2) //(5) [5, 4, 3, 2, 1]
console.log(res) //(5) [5, 4, 3, 2, 1]


//8.sort() 数组排序
//第一种使用方式 是按照一位一位来看的
//语法:数组.sort()
//直接改变原始数组
//返回值:排序后的数组 与arr3一样
var arr3=[1,3,5,9,8,7,6,11,2,555,8,10,13]
var res=arr3.sort()
console.log(arr3) //(13) [1, 10, 11, 13, 2, 3, 5, 555, 6, 7, 8, 8, 9]
console.log(res) //(13) [1, 10, 11, 13, 2, 3, 5, 555, 6, 7, 8, 8, 9]

//第二种使用方式 按照数字大小排列
//语法:数组.sort(function(a,b){return a-b})
//升序:a-b
//降序:b-a
//直接改变原始数组
//返回值:排序后的数组 与arr3一样
var res=arr3.sort(function(a,b){
return a-b
})
console.log(arr3) //(13) [1, 2, 3, 5, 6, 7, 8, 8, 9, 10, 11, 13, 555]
console.log(res) //(13) [1, 2, 3, 5, 6, 7, 8, 8, 9, 10, 11, 13, 555]


//9.join() 把数组链接成字符串
//语法:数组.join('以什么字符链接') 传递的参数可不定,默认为逗号链接
//不改变原始数组
//返回值:就是用指定字符链接好的字符串
// var res=arr.join('--')
// console.log(arr) //(4) ["hello", "world", "sql", "linux"]
// console.log(res) //hello--world--sql--linux
// console.log(typeof res) //string


//10.slice() 截取数组
//语法:数组.slice(开始的索引,结束的索引) 包前不包后
//不改变原始数组
//以数组的形式返回截的内容
var res=arr.slice(1,3)
console.log(arr) //(4) ["hello", "world", "sql", "linux"]
console.log(res) //(2) ["world", "sql"]

//第二个参数不写 从开始索引截取到末尾
var res=arr.slice(1)
console.log(arr) //(4) ["hello", "world", "sql", "linux"]
console.log(res) //(3) ["world", "sql", "linux"]


//传递负数函数时,把这个数字和数组的length相加得到的结果 4+(-1) ==3
var res=arr.slice(1,-1) //等价于 slice(1,3)
console.log(arr) //(4) ["hello", "world", "sql", "linux"]
console.log(res) //(2) ["world", "sql"]

</script>

原文地址:https://www.cnblogs.com/d534/p/12710217.html