JavaScript的数组常⽤⽅法数组常⽤⽅法
1. push
=> 语法 arr.push(数据1, 数据2, 数据3, ...)
=> 作⽤把所有的参数按照顺序追加到数组的末尾
=> 返回值,追加以后数组的长度
=> 直接操作原始数组
2. pop
=> 语法数组.pop()
=> 作⽤删除数组的最后⼀个数据
javascript全局数组
=> 返回值被删除的数据
=> 直接操作原始数组
3. unshift()
=> 语法数组.unshift(数据1, 数据2, 数据3, ...)
=> 作⽤从数组最前⾯插⼊⼀些数据
=> 返回值插⼊后的数组长度
=> 直接操作原始数组
4. shift()
=> 语法数组.unshift()
=> 作⽤删除数组的最前⾯⼀个元素
=> 返回值被删除的数据
=> 直接操作原始数组
5. reverse()
=> 语法数组.reverse()
=> 作⽤反转数组
=> 返回值反转后的数组
=> 直接操作原始数组
6. sort()
=> 语法数组.sort()
1. arr.sort()
-> 按照每⼀个数据中的每⼀位数组的ASCII码进⾏排列
2. arr.sort(function (a, b) {return a -b})
-> 从⼩到⼤排
2. arr.sort(function (a, b) {return b -a})
-
> 从⼤到⼩排
=> 作⽤数组排序
=> 返回值排序后的数组
=> 直接操作原始数组
7. splice()
=> 语法数组.splice(开始索引,多少个)
1. 数组.splice(开始索引,多少个)
-> 从开始索引,截取多少个
-> 第⼆个参数可以不写,直接到末尾
2. 数组.splice(开始索引,多少个,替换数组1,替换数组2,替换数组3, ...)    -> 把替换数据按照顺序插⼊到你街区的位置
-> 注意从那个位置开始索引删除,就替换数据的第⼀个就插⼊到哪个位置=> 作⽤
1. 截取数组
2. 替换新内容
=> 返回值⼀定是⼀个数组
=> 如果你截取⼀个数据,数组⾥⾯有⼀个
=> 如果你⼀个都不截取,那么是⼀个空数组
=> 直接操作原始数组
8. concat()
=> 语法数组.concat(数据1,数据2)
=> 作⽤
-> 如果参数是数组,那么把数组拆开,⾥⾯每⼀项追加到原数组后⾯
-> 如果是参数数据,那么直接追加
=> 返回值追加好的数组
=> 不改变原始数组
9. slice()
=> 语法
1. 数组.slice(开始索引, 结束索引) [)
第⼀个参数可以不写,表⽰从头
第⼆个参数可以不写,表⽰到尾
2. 数组.slice(开始索引, 结束索引) [)
-> 参数可以写⼀个负整数
-> length - 负数
=> 作⽤获取数组⾥的某些数据
=> 返回值⼀个数组
-> 如果你获取多个数据,数组⾥⾯有多个
-> 如果你获取⼀个数据,数组⾥⾯有⼀个
-> 如果你⼀个都不获取,那么是个空数组
=> 不改变原始数组
10. join()
=> 语法:数组.join("连接符号")
-> 不传递参数是按照(,)连接
-> 你传递什么按照什么连接
=> 作⽤:把数字⾥⾯得每⼀个数据使⽤连接符号连接在⼀起
=> 返回值:是⼀个连接好的内容,是⼀个String 类型
11. indexOf()
=> 语法:
-> 数组.indexOf(数据)
-> 数组.indexOf(数据, 开始索引) 从哪个索引开始查
=> 作⽤:正向查数组⾥⾯指定这个数据的索引
=> 返回值:
-> 如果有这个数据,是第⼀个满⾜条件的数据的索引
-> 如果没有这个数据,那么是 -1
12. lastIndexOf()
=> 语法:
-> 数组.lastIndexOf(数据)
-
> 数组.lastIndexOf(数据, 开始索引) 从哪个索引开始往前查=> 作⽤:反向查数组⾥⾯指定这个数据的索引
=> 返回值:
-> 如果有这个数据,是第⼀个满⾜条件的数据的索引
-> 如果没有这个数据,那么是 -1
-> 虽然是从后往前索引,但是索引还是正常索引
13. forEach()
=> 语法:数组.forEach(function (item, index, arr){})
-> item 数组的每⼀项
-> index 数组的索引
-> arr 原始数组
=> 作⽤:取代for循环的作⽤,遍历数组
=> 没有返回值
13. map()
=> 语法:数组.map(function (item, index, arr){})
-> item 数组的每⼀项
-> index 数组的索引
-> arr 原始数组
=> 作⽤:映射数组
=> 返回值:
-> 新的数组
-> ⾥⾯是对原始数组每⼀个数据的操作
-> 返回值数组,⼀定和原始数组长度⼀样
=> 不改变原始数组
对应数组操作代码
// 1.push
var arr =["hello","world","你好","世界"]
var res = arr.push("新来的","新来的2","新来的3")
console.log(res);
console.log(arr);
//  2.pop
var arr =["hello","world","你好","世界"]
var res = arr.pop();
console.log(res);
console.log(arr);
// 3. unshift
var arr =["hello","world","你好","世界"]
var res = arr.unshift("新来的1","新来的2");
console.log(res);
console.log(arr);
// 4. shift
var arr =["hello","world","你好","世界"]
var res = arr.shift("新来的1","新来的2");
console.log(res);
console.log(arr);
/
/ 5. reverse()
var arr =["hello","world","你好","世界"]
var res = verse();
console.log(res);
console.log(arr);
// 6. sort()
var arr =[1,11,133,26,51,19,32,27,15]
var res = arr.sort();
var res = arr.sort();
console.log(res);
console.log(arr);
var res = arr.sort(function(a, b){
return a - b
})
console.log(res);
console.log(arr);
var res = arr.sort(function(a, b){
return b - a
})
console.log(res);
console.log(arr);
// 7. splice()
var arr =[1,11,133,26,51,19,32,27,15]
// var res = arr.splice(1, 3);
// console.log(res);
// console.log(arr);
var res = arr.splice(1,0,"新来的","新来的⼆");    console.log(res);
console.log(arr);
// 8. concat()
var arr =[1,11,133,26,51,19,32,27,15]
var res = at([10,20],[30,40],100);    console.log(res);
console.log(arr);
// 9. slice
var arr =[1,11,133,26,51,19,32,27,15]
var res = arr.slice(1,3);
console.log(res);
console.log(arr);
var res2 = arr.slice(-8,-6);
console.log(res2);
console.log(arr);
// 10. join()
var arr =[1,11,133,26,51,19,32,27,15]
var res = arr.join("@-@");
var res = arr.join();
console.log(res);
console.log(arr);
// 11. indexOf()
var arr =[1,11,133,26,51,19,32,11,15]
var res = arr.indexOf(11);
console.log(res);
console.log(arr);
var res = arr.indexOf(11,4);
console.log(res);
console.log(arr);
// 12. LastIndexOf()
var arr =[1,11,133,26,51,19,32,11,15]
var arr =[1,11,133,26,51,19,32,11,15]
var res = arr.lastIndexOf(11);
console.log(res);
console.log(arr);
var res = arr.lastIndexOf(11,5);
console.log(res);
console.log(arr);
// 13. forEach()
var arr =[1,11,133,26,51,19,32,11,15]
let res = arr.forEach(function(item, index, arr){
/
/ 这个函数,会根据数组⾥⾯有多少个数据执⾏多少回// console.log("我执⾏了")
// item 分别是这个数组⾥⾯的每⼀项
console.log(item);
console.log(index);
console.log(arr);
})
// 14. map()
var arr =[1,11,133,26,51,19,32,11,15]
var res = arr.map(function(item, index, arr){
console.log(item,"-----", index,"-----", arr);
return item *1.3;
})
console.log(res);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。