文章目录
一、一维数组
1.1 创建数组
1.1.1 直接创建
语法:var arrayName = [元素1,元素2,…,元素n];
var stu = ["张三","李四","王五","赵六","卢大"];
语法:var 数组名=new Array(元素1,元素2,…,元素n);
var stu = new Array("张三","李四","王五","赵六","卢大");
1.1.2 先声明后赋值
语法:var 数组名=new Array();
或者 var 数组名=new Array(length);
var stu = new Array();
// 或者 var stu = new Array(5);
stu[0] = "张三";
// 元素赋值
stu[1] = "李四";
stu[2] = "王五";
stu[3] = "赵六";
stu[4] = "卢大";
1.2 获取
1.2.1 数组长度——length
语法:数组名.length
var stu.length;
1.2.2 数组元素——[]
情况一:只取用一个元素。
语法:数组名[index]
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu[2];
document.write(stu);
// 结果为:stu = ["张三","李四","王五","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = "王五"
情况二:向后取用n个。
语法:数组名.splice(index, n)
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.splice(2, 2);
document.write(stu);
// 结果为:stu = ["张三","李四","卢大"]
document.write(stu2);
// 结果为:stu2 = ["王五","赵六"],此时stu2为数组。
情况三:向前取用n个。
语法:数组名.splice((index + 1)-n, n)
注意:(index + 1)-n >= 0
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.splice((2+1)-2, 2);
document.write(stu);
// 结果为:stu = ["张三","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = ["李四","王五"],此时stu2为数组。
情况四:知道开始索引和结束索引。
语法:数组名.slice(start, end)
注意:遵循[start, end) 原理
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.slice(1, 3);
document.write(stu);
// 结果为:stu = ["张三","李四","王五","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = ["李四","王五"],此时stu2为数组。
1.2.3 数组元素索引值——indexOf
语法:数组名.indexOf(“数组中的元素”)
var stu = new Array("张三","李四","王五","赵六","卢大");
var wangWuIndex = stu.indexOf("王五");
document.write(wangWuIndex);
// 结果为:wangWuIndex = 2
for(i = 0; i < arrayName.length; i++){
arrayName[i].index=i;
}
1.3 增加元素——splice
1.3.1 最前面增加——unshift
语法:数组名.unshift(“元素1”,“元素2”)
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.unshift("钱二","陆七");
document.write(stu);
// 结果为:stu=["钱二","陆七","张三","李四","王五","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = 7
1.3.2 任意位置增加——splice
语法:数组名.splice(插入位置, 0, “元素1”, “元素2”);
注意:该位置原有元素,会自动向后移动一位。
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.splice(2, 0, "钱二", "陆七");
document.write(stu);
// 结果为:stu=["张三","李四","钱二","陆七","王五","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = ""
1.3.3 最后面增加——push
语法:数组名.push(“元素1”,“元素2”)
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.push("钱二","陆七");
document.write(stu);
// 结果为:stu=["张三","李四","王五","赵六","卢大","钱二","陆七"]
document.write(stu2);
// 结果为:stu2 = 7
1.4 删除元素——splice
1.4.1 最前面删除——splice
语法:数组名.splice(0, 1);
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.splice(0, 1);
document.write(stu);
// 结果为:stu=["李四","王五","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = "张三"
1.4.2 任意位置删除——splice
情况一:向后删除
语法:数组名.splice(index, 向后删除元素个数);
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.splice(2, 2);
document.write(stu);
// 结果为:stu=["张三","李四","卢大"]
document.write(stu2);
// 结果为:stu2 = ["王五","赵六"]
情况二:向前删除
语法:数组名.splice((index + 1) -delForwardNumber, delForwardNumber)
注意:((index + 1) - delForwardNumber) >= 0
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.splice((2+1)-2, 2);
document.write(stu);
// 结果为:stu=["张三","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = ["李四","王五"]
1.4.3 最后面删除——pop
语法:数组名.pop()
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.pop();
document.write(stu);
// 结果为:stu=["张三","李四","王五","赵六"]
document.write(stu2);
// 结果为:stu2 = "卢大"
1.5 替换元素——splice
1.5.1 位置知
语法:数组名.splice(插入位置, 向后删除元素数量, “元素1”, “元素2”);
var stu = new Array("张三","李四","王五","赵六","卢大");
var stu2 = stu.splice(2, 1, "钱二", "陆七");
document.write(stu);
// 结果为:stu=["张三","李四","钱二","陆七","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = "王五"
1.5.2 元素知
语法:数组名.splice(插入位置, 向后删除元素数量, “元素1”, “元素2”);
var stu = new Array("张三","李四","王五","赵六","卢大");
var index = stu.indexOf("王五");
var stu2 = stu.splice(index, 1, "钱二", "陆七");
document.write(stu);
// 结果为:stu=["张三","李四","钱二","陆七","赵六","卢大"]
document.write(stu2);
// 结果为:stu2 = "王五"
1.6 数组拼接——concat
语法:新数组对象.concat(数组1,数组2,数组3,数组4,…,数组n,)
var stu1 = new Array("张三","李四");
var stu2 = new Array("王五","赵六","卢大");
var stu3 = new Array("钱二", "陆七");
stu1.concat(stu2, stu3);
document.write(stu1);
// 结果为:stu1=["张三","李四","王五","赵六","卢大","钱二", "陆七"]
1.7 数组转化字符串——join
语法:数组名.join("")
var stu1 = ["张三","李四","王五","赵六","卢大","钱二", "陆七"];
var str = stu1.join("");
document.write(stu1);
// 结果为:stu1 = ["张三","李四","王五","赵六","卢大","钱二", "陆七"];
document.write(str);
// 结果为:str = "张三李四王五赵六卢大钱二陆七"
1.8 数组排序
1.8.1 倒序排列-reverse
语法: 数组名.reverse();
var stu1 = ["张三","李四","王五","赵六","卢大","钱二", "陆七"];
var stu2 = stu1.reverse();
document.write(stu1);
// 结果为:stu1 = ["张三","李四","王五","赵六","卢大","钱二", "陆七"];
document.write(stu2);
// 结果为:str = ["陆七","钱二","卢大","赵六","王五","李四","张三"]
1.8.2 升降排序-sort
语法:数组名.sort()
var stu1 = ["张三","李四","王五","赵六","卢大","钱二", "陆七"];
//var stu2 = stu1.sort();
//document.write(stu1);
// 卢大,张三,李四,王五,赵六,钱二,陆七
//document.write(stu2);
// 卢大,张三,李四,王五,赵六,钱二,陆七
function sortNum1(a,b){return a-b;}
function sortNum2(a,b){return b-a;}
//var stu3 = stu1.sort(sortNum1);
//document.write(stu3);
// 张三,李四,王五,赵六,卢大,钱二,陆七
var stu4 = stu1.sort(sortNum2);
document.write(stu4);
// 张三,李四,王五,赵六,卢大,钱二,陆七
// ====================================
var stu1 = [1,3,0,5,3,8,9];
var stu2 = stu1.sort();
document.write(stu1);
// 0,1,3,3,5,8,9
//document.write(stu2);
// 0,1,3,3,5,8,9
//function sortNum1(a,b){return a-b;}
// 或stu1.sort(function(a,b){return a-b;});
//function sortNum2(a,b){return b-a;}
// 或stu1.sort(function(a,b){return b-a;});
//var stu3 = stu1.sort(sortNum1);
//document.write(stu3);
// 0,1,3,3,5,8,9
//var stu4 = stu1.sort(sortNum2);
//document.write(stu4);
// 9,8,5,3,3,1,0
二、二维数组
2.1 创建数组
2.1.1 直接创建
语法:var arrayName = [元素1,元素2,…,元素n];
var stu = [["张三","李四","王五","赵六","卢大"],["张三","李四","王五","赵六","卢大"],["张三","李四","王五","赵六","卢大"]];
2.1.2 先声明后赋值
语法:var 数组名=new Array();
// 先声明一维数组。
var stu = new Array(3);
// 再声明每个一维数组的元素为一个一维数组。注意位置。
for(var k=0;k<3;k++){
stu[k]=new Array();
}
// 给二维数组赋值。
stu[0][0] = "张三",stu[0][1] = "李四",stu[0][2] = "王五",stu[0][3] = "赵六",stu[0][4] = "卢大";
2.2 获取
2.2.1 元素-[index]
语法:arrayName[index][index];
// 字符串
stu[2][1]
// 数组
stu[2]
2.2.2 元素索引值-indexOf
语法:arrayName[index].indexOf(“查找字符串”);
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
document.write(stu[0].indexOf("李四"));
// 结果为:1
document.write(stu[0].indexOf("李四3"));
// 结果为:-1
document.write(stu[2].indexOf("李四3"));
// 结果为:1
2.3 增加元素——splice
2.3.1 开头-unshift
// ======================数组=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.unshift(["钱二","陆七"]);
console.log(stu1);
// 结果为:4
console.log(stu);
// 结果为:stu = [["钱二","陆七"],["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
// ======================字符串=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[0].unshift("钱二","陆七");
console.log(stu1);
// 结果为:7
console.log(stu);
// 结果为:stu = [["钱二","陆七","张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.3.2 任意位置-splice
// ======================数组=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.splice(1, 0, ["钱二","陆七"]);
console.log(stu1);
// 结果为:[]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["钱二","陆七"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
// ======================字符串=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[1].splice(1, 0, "钱二","陆七");
console.log(stu1);
// 结果为:[]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","钱二","陆七","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.3.3 末尾-push
// ======================数组=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.push(["钱二","陆七"]);
console.log(stu1);
// 结果为:4
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"],["钱二","陆七"]];
// ======================字符串=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[0].push("钱二","陆七");
console.log(stu1);
// 结果为:7
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大","钱二","陆七"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.4 删除元素——splice
2.4.1 开头
// ======================数组=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.splice(0, 1);
console.log(stu1);
// 结果为:stu1 = ["张三","李四","王五","赵六","卢大"]
console.log(stu);
// 结果为:stu = [["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
// ======================字符串=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[0].splice(1, 1);
console.log(stu1);
// 结果为:stu1 = ["李四"]
console.log(stu);
// 结果为:stu = [["张三","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.4.2 任意位置
// ======================数组=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.splice(1, 1);
console.log(stu1);
// 结果为:stu1 = ["张三2","李四2","王五2","赵六2","卢大2"]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三3","李四3","王五3","赵六3","卢大3"]];
// ======================字符串=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[1].splice(1, 1);
console.log(stu1);
// 结果为:stu1 = ["李四2"]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.4.3 末尾
// ======================数组=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.splice(2, 1);
console.log(stu1);
// 结果为:stu1 = ["张三3","李四3","王五3","赵六3","卢大3"]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"]]
// ======================字符串=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[0].splice(4, 1);
console.log(stu1);
// 结果为:stu1 = ["卢大"]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.5 替换元素——splice
// ======================数组=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.splice(1, 1, ["钱二","陆七"]);
console.log(stu1);
// 结果为:stu1 = ["张三2","李四2","王五2","赵六2","卢大2"]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["钱二","陆七"],["张三3","李四3","王五3","赵六3","卢大3"]];
stu.splice(1, 1, stu1);
console.log(stu);
stu = [["张三","李四","王五","赵六","卢大"],["钱二","陆七"],["张三3","李四3","王五3","赵六3","卢大3"]];
// ======================字符串=================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[0].splice(1, 1, "钱二","陆七");
console.log(stu1);
// 结果为:stu1 = ["李四"]
console.log(stu);
// 结果为:stu = [["张三","钱二","陆七","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.6 数组拼接——concat+splice
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu4 = ["钱二4","陆七4"];
var stu5 = ["钱二5","陆七5"];
var stu1 = stu[1].concat(stu4, stu5);
console.log(stu1);
// 结果为:stu1 = ["张三2","李四2","王五2","赵六2","卢大2","钱二4","陆七4","钱二5","陆七5"]
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
stu.splice(1, 1, stu1);
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2","钱二4","陆七4","钱二5","陆七5"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.7 数组转化字符串——join+replace
// ======================数组——join + replace =================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 =stu.join("");
console.log(stu1);
// 结果为:stu1 = "张三,李四,王五,赵六,卢大张三2,李四2,王五2,赵六2,卢大2张三3,李四3,王五3,赵六3,卢大3"
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu2 = stu1.replace(/,/g, "");
console.log(stu2);
//stu2 = "张三李四王五赵六卢大张三2李四2王五2赵六2卢大2张三3李四3王五3赵六3卢大3"
console.log(stu1);
//stu1 = "张三,李四,王五,赵六,卢大张三2,李四2,王五2,赵六2,卢大2张三3,李四3,王五3,赵六3,卢大3"
// ======================字符串——join =================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[1].join("");
console.log(stu1);
// 结果为:stu1 = "张三2李四2王五2赵六2卢大2"
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
2.8 数组排序
2.8.1 倒序排列-reverse
语法: 数组名.reverse();
// ====================== 数组 =================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.reverse();
console.log(stu1);
// 结果为:stu1 = [["张三3","李四3","王五3","赵六3","卢大3"],["张三2","李四2","王五2","赵六2","卢大2"],["张三","李四","王五","赵六","卢大"]];
console.log(stu);
// 结果为:stu = [["张三3","李四3","王五3","赵六3","卢大3"],["张三2","李四2","王五2","赵六2","卢大2"],["张三","李四","王五","赵六","卢大"]];
// ====================== 字符串 =================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[0].reverse();
console.log(stu1);
// 结果为:stu1 = ["卢大", "赵六", "王五", "李四", "张三"]
console.log(stu);
// 结果为:stu = [["卢大", "赵六", "王五", "李四", "张三"],["张三2", "李四2", "王五2", "赵六2", "卢大2"],["张三3", "李四3", "王五3", "赵六3", "卢大3"]];
2.8.2 升降排序-sort
语法:数组名.sort()
// ====================== 数组 =================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu.sort();
console.log(stu1);
// 结果为:stu1 = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
console.log(stu);
// 结果为:stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
// ====================== 字符串 =================
var stu = [["张三","李四","王五","赵六","卢大"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
var stu1 = stu[0].sort();
console.log(stu1);
// 结果为:stu1 = ["卢大", "张三", "李四", "王五", "赵六"]
console.log(stu);
// 结果为:stu = [["卢大", "张三", "李四", "王五", "赵六"],["张三2","李四2","王五2","赵六2","卢大2"],["张三3","李四3","王五3","赵六3","卢大3"]];
// ====================== 数组 =================
var stu = [[1,2,3,4,5],[6,7,8,10,9],[11,13,15,16,17]];
var stu1 = stu.sort();
console.log(stu1);
// 结果为:stu1 = [[1,2,3,4,5],[11,13,15,16,17],[6,7,8,10,9]];
console.log(stu);
// 结果为:stu = [[1,2,3,4,5],[11,13,15,16,17],[6,7,8,10,9]];
// ====================== 字符串 =================
var stu = [[1,2,3,4,5],[6,7,8,10,9],[11,13,15,16,17]];
var stu1 = stu[0].sort();
console.log(stu1);
// 结果为:stu1 = [10, 6, 7, 8, 9]
console.log(stu);
// 结果为:stu = [[1,2,3,4,5],[10, 6, 7, 8, 9],[11,13,15,16,17]];
function sortNum1(a,b){return a-b;}
function sortNum2(a,b){return b-a;}
// ====================== 升序 =================
var stu = [[1,2,3,4,5],[6,7,8,10,9],[11,13,15,16,17]];
var stu1 = stu[1].sort(sortNum1);
console.log(stu1);
// 结果为:stu1 = [6, 7, 8, 9, 10]
console.log(stu);
// 结果为:stu = [[1,2,3,4,5],[6, 7, 8, 9, 10],[11,13,15,16,17]];
// ====================== 降序 =================
var stu = [[1,2,3,4,5],[6,7,8,10,9],[11,13,15,16,17]];
var stu1 = stu[1].sort(sortNum2);
console.log(stu1);
// 结果为:stu1 = [10, 9, 8, 7, 6]
console.log(stu);
// 结果为:stu = [[1,2,3,4,5],[10, 9, 8, 7, 6],[11,13,15,16,17]];