数组(Array)

李艳生

湖北师范大学

物理与电子科学学院

2020年春季

参考文献

JavaScript.info

数组概念

  • 一组数据的有序集合
  • 每个元素都有一个数字下标
  • 数组元素的下标从0开始
  • 最简单的内存数据结构

数组操作

  • 数组创建
  • 数组访问
  • 数组遍历
  • 数组增删
  • 数组截取
  • 数组合并
  • 数组排序
  • 数组搜索
  • 数组检查
  • MapReduce
  • 数组转换

数组创建

  • new Array()
  • 数组常量[]

// 创建空数组
let a1 = new Array();
let a2 = [];

// 创建并初始化数组
let a3 = new Array(1,2,3,4,5);
let a4 = [1,2,3,4,5];

数组元素访问

  • 数组下标从0开始
  • 数组元素用下标访问

// 创建并初始化数组
let a3 = new Array(1,2,3,4,5);
let a4 = [1,2,3,4,5];
//数组访问
console.log(a3[0]);
console.log(a4[0]);

数组传统遍历

  • 数组下标从0开始
  • 传统遍历利用length属性

// 创建并初始化数组
let a3 = new Array(1,2,3,4,5);
let a4 = [1,2,3,4,5];
//数组遍历
for(let i = 0; i < a3.length; i++){
    console.log(a3[i]);
}

数组迭代遍历

  • 迭代遍历for...of...
  • 迭代遍历只能访问元素,不能访问下标

// 创建并初始化数组
let a4 = [1,2,3,4,5];
//数组遍历
for(let n of a4){
    console.log(n);
}

数组forEach遍历

  • arr.forEach(function(item, index, array){})
  • 对arr数组每个元素执行函数

// 创建并初始化数组
let a4 = [1,2,3,4,5];
//数组遍历
a4.forEach(function(item, index, array){
    console.log(item)
})

数组尾部增删元素

  • push(...items)在尾部添加元素
  • pop在尾部删除元素

// 创建并初始化数组
let a4 = [1,2,3,4,5];
//在尾部删除元素
console.log(a4.pop());
//在尾部插入元素
a4.push(6,7);
//数组遍历
for(let n of a4){
    console.log(n);
}

数组头部增删元素

  • unshift(...items)头部添加元素
  • shift尾部删除元素

// 创建并初始化数组
let a4 = [1,2,3,4,5];
//在头部删除元素
console.log(a4.shift());
//在尾部插入元素
a4.unshift(6,7);
//数组遍历
for(let n of a4){
    console.log(n);
}

任意位置增删元素

  • splice(index[,deleteCount,elem1,...,elemn])
  • 从数组下标index开始
  • 删除deleteCount个元素
  • 并在当前位置插入elem1,...,elemn元素
  • 返回已删除元素数组

任意位置增删元素


// 创建并初始化数组
let a4 = [1,2,3,4,5];
let removed = a4.splice(1,2,6,7,8);
//数组遍历
for(let n of a4){
    console.log(n);
}
for(let m of removed){
    console.log(m)
}

数组截取

  • slice(index[,end])
  • 从数组下标index开始
  • 截取到下标end之前元素

// 创建并初始化数组
let a4 = [1,2,3,4,5];
let sliced = a4.slice(1,3);
//数组遍历
for(let n of a4){
    console.log(n);
}
for(let m of sliced){
    console.log(m)
}

数组合并

  • arr.concat(arg1, ...arg2)
  • 合并arr,arg1,arg2i一个新数组

// 创建并初始化数组
let a4 = [1,2,3,4,5];
let a5 = a4.concat([6,7],[8,9]);
//数组遍历
for(let n of a4){
    console.log(n);
}
for(let m of a5){
    console.log(m)
}

数组排序

  • sort方法默认按字母顺序排序
  • 可自定义排序

// 创建并初始化数组
let a4 = [1,15,2];
a4.sort();
//数组遍历
a4.forEach(function(item, index, array){
    console.log(item)
})
a4.sort(function(a,b){return a-b;});
a4.forEach(function(item, index, array){
    console.log(item)
})

数组搜索

  • indexOf返回第一个找到元素下标,lastIndexOf
  • find返回第一个找到的元素,findIndex
  • includes找到返回true
  • filter返回所有元素

数组搜索


// 创建并初始化数组
let a4 = [1,15,2,15];
console.log(a4.indexOf(15));
console.log(a4.lastIndexOf(15));
console.log(a4.indexOf(50));
let r1 = a4.find(function(item, index, array){
    return item == 15;
});
console.log(r1);
let r2 = a4.findIndex(function(item, index, array){
    return item == 15;
});
console.log(r2);
let r3 = a4.filter(function(item, index, array){
    return item == 15;
});
console.log(r3);
console.log(a4.includes(15));

数组检查

  • arr.every(fn)所有满足条件返回true,否则返回false
  • arr.some(fn)只要一个满足返回true,否则返回false

// 创建并初始化数组
let a4 = [1,2,3,4,5];
let r1 = a4.every(x=>x%2==0);
console.log(r1);
let r2 = a4.some(x=>x%2==0);
console.log(r2);

mapreduce

  • arr.map(fn)每个元素执行fn,返回结果新数组
  • arr.reduce(fn,init)每个元素执行fn,每次调用返回中间结果

// 创建并初始化数组
let a4 = [1,2,3,4,5];
let r1 = a4.map(x=>x*2);
for(let n of r1){
    console.log(n);
}

let r2 = a4.reduce((sum, current)=>sum + current,0);
console.log(r2);

数组转换

  • str.split(delim)用delim分隔符切分字符串str成数组
  • arr.join(delim)用delim把arr连接成字符串

let str = '1,2,3,4,5';
let a1 = str.split('\\,');
for(let n of a1){
    console.log(n);
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
let r1 = a4.join('-');
console.log(r1);

多维数组

  • 数组元素还是数组构成多维数组
  • 数组的数组,数组嵌套

let a2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
alert(a2[1][1]);

任务

随机创建某个班某门课程100人成绩,1.计算平均成绩,2.统计优良中合格不合格人数,3.按从高分到低分排名。