创建数组
方法一:(常用)
var arr = [];
方法二:
var numbers = new Array();
判断是否是数组:
var arr = [];
var str = "";
console.log(Array.isArray(arr));//true
console.log(Array.isArray(str));//false
字符串生成数组(split)
var sentence = "a b c d e";
var words = sentence.split(" ");
for (let i = 0; i < words.length; i++) {
console.log("word"+i+":"+words[i]);
}
//word0:a
//word1:b
//word2:c
//word3:d
// word4:e
数组的字符串表示(join()、toString())
var names = ["David","lisi","Cynthia","zhangsan","lisi","wangwu","lisi"]
var namestr = names.join()
console.log(namestr)
var namestr1 = names.toString();
console.log(namestr1)
数组整体性操作
浅复制:
把一个数组赋给另一个数组时,只是为被赋值的数组增加了一个新的引用。通过原引用修改了数组的值,另外一个引用也会感知到这个变化。如下,修改arr,newArr的值也会变。
var arr = [1,2,3,4,5];
var newArr = arr;
arr[0] = 400;
console.log(newArr);//[400,2,3,4,5]
深复制:
var arr = [1,2,3,4,5];
var newArr = [];
for (let i = 0; i < arr.length; i++) {
newArr[i] = arr[i]
}
arr[0] = 400;
console.log(newArr)//[1, 2, 3, 4, 5]
查找元素(indexof())
var names = ["David","Cynthia","zhangsan","lisi","wangwu"]
console.log(names.indexOf("lisi"));//3
console.log(names.indexOf("lisi111"));//-1
indexOf()返回第一个与参数相同的元素的索引,lastIndexOf()返回相同元素中最后一个元素索引。
var names = ["David","lisi","Cynthia","zhangsan","lisi","wangwu","lisi"]
console.log(names.indexOf("lisi"));//1
console.log(names.lastIndexOf("lisi"));//6
为数组创建新数组(concat()、splice())
var names = ["David","Cynthia","zhangsan","lisi","wangwu"];
var names1 = ["wangwu","zhaoliu"];
var newNames = names.concat(names1)//['David', 'Cynthia', 'zhangsan', 'lisi', 'wangwu', 'wangwu', 'zhaoliu']
var newNames1 = newNames.splice(3,3)//['lisi', 'wangwu', 'wangwu']
splice为数组增加元素
var arr = [1,2,3,4,5];
arr.splice(3,0,9,8,7)//[1, 2, 3, 9, 8, 7, 4, 5]
splice删除数组中的元素
var arr = [1,2,3,4,5,6,7,8];
arr.splice(3,3)//[1, 2, 3, 7, 8]
操作数组
数组末尾添加元素(push)
var arr = [1,2,3,4,5,6,7,8];
arr.push(100)//[1,2,3,4,5,6,7,8,100]
数组开头添加一个元素(unshift)
arr.unshift(100)//[100, 1, 2, 3, 4, 5, 6, 7, 8]
删除数组末尾的元素(pop)
arr.pop()//[1, 2, 3, 4, 5, 6, 7]
删除数组的第一个元素
arr.shift()//[2, 3, 4, 5, 6, 7, 8]
为数组排序
反转数组(reverse)
var arr = [1,2,3,4,5,6,7,8];
arr.reverse()//[8, 7, 6, 5, 4, 3, 2, 1]
按照字典顺序对元素进行排序(sort)
var names = ["David","Cynthia","Zhangsan","Lisi","Wangwu"];
var newNames = names.sort();
console.log(newNames);//['Cynthia', 'David', 'Lisi', 'Wangwu', 'Zhangsan']
为数字元素排序
var arr = [3,1,10,4,5,90,7,8];
function compare(num1,num2){
return num1 - num2;
}
arr.sort(compare)
console.log(arr)//[1, 3, 4, 5, 7, 8, 10, 90]
不生成新数组的迭代器方法
forEach接收函数作为参数
var arr = [3,1,10,4,5,90,7,8];
function square(num){
console.log(num,num*num)
}
arr.forEach(square)
every方法接收一个返回值为布尔类型的函数,所有元素均返回true,该方法才会返回true
var arr = [3,1,10,4,5,90,7,8];
var arr1 = [2,10,4,8,20];
function isEven(num){
return num % 2 == 0;
}
var even = arr.every(isEven)
var even1 = arr1.every(isEven)
console.log(even,even1)//false,true
some方法和every相似,接受一个返回值为布尔类型的函数,只要有一个元素使得该函数返回true,该方法就返回true
var arr = [3,1,10,4,5,90,7,8];
var arr1 = [1,21,43,89,21];
function isEven(num){
return num % 2 == 0;
}
var even = arr.some(isEven)
var even1 = arr1.some(isEven)
console.log(even,even1)//true false
reduce从一个累加值开始,不断对累加值和数组中的后续元素调用该函数,最后返回得到的累加值。
var arr = [3,1,10,4,5,90,7,8];
function add(runningTotal,curValue){
return runningTotal+curValue;
}
var sum = arr.reduce(add);
console.log(sum)//128
reduceRight和reduce一样,只是从右到左执行
var names = ["David ","Cynthia ","Zhangsan ","Lisi ","Wangwu "];
function concat(str,item){
return str+item;
}
var sentence = names.reduceRight(concat)
console.log(sentence)//Wangwu Lisi Zhangsan Cynthia David
生成新数组的迭代器方法
map和forEach有点儿像,区别就是map返回一个新的数组。
var arr = [3,1,10,4,5,90,7,8];
function curve(grade){
return grade+5;
}
var newgrades = arr.map(curve);
console.log(newgrades)//[8, 6, 15, 9, 10, 95, 12, 13]
filter和every类似,传入一个返回值为布尔类型的函数,和every方法不同的是,当对数组中的所有元素应用该函数,结果均为true,该方法并不返回true,而是返回一个新数组,新数组中包含结果为true的元素。
var arr = [1,2,21,43,89,21,2,10];
function isEven(num){
return num % 2 == 0;
}
function isOdd(num){
return num % 2 != 0;
}
var even = arr.filter(isEven)//[2, 2, 10]
var even1 = arr.filter(isOdd)//[1, 21, 43, 89, 21]
console.log(even,even1)