数组
将一组相关的数据一起存放,并提供方便的访问方式
就是一组数据的集合,存在单个变量下
数组创建方法
1.new关键字 2.数组字面量创建数组(常用)
new关键字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = new Array();
</script>
</head>
<body>
</body>
</html>
数组字面量创建数组 字面量—> [ ]
//数组字面量创建数组
var arr = [];
数组里面的数据 用 逗号 隔开
数组的初始化是 声明数组并赋值
访问数组元素
1.索引(下标):访问数组元素的下标(从0开始)
// 访问第三个元素
console.log(arr1[2]);
2.遍历数组 ————> 循环来实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = ['red', 'green', 'blue'];
for (var i = 0; i < 3; i++) {
console.log(arr[i]);
}
</script>
</head>
<body>
</body>
</html>
数组长度
数组名.length
可以动态检查数组的长度(数组元素个数)
console.log(arr.length);
数组求和平均值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = [2, 6, 1, 7, 4];
var sum = 0;
var arrage = 0;
for (i = 0; i < arr.length; i++) {
sum += arr[i];
}
arrage = sum / arr.length;
console.log(sum);
console.log(arrage);
</script>
</head>
<body>
</body>
</html>
数组转换为字符串
将数组[‘red’,‘green’,‘blue’,‘pink’] 转换为字符串 ,并且用 | 或其他符号分割
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = ['red', 'green', 'blue', 'pink'];
var str = '';
var sep = '*';
for (var i = 0; i < arr.length; i++) {
str += arr[i] + sep;
}
console.log(str);
</script>
</head>
<body>
</body>
</html>
数组中新增元素
1.修改length长度 2.增加索引号
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = ['red', 'blue', 'green'];
console.log(arr.length);
arr.length = 5;
console.log(arr);
console.log(arr[3]);
console.log(arr[4]);
var arr1 = ['red', 'blue', 'green'];
arr1[3] = 'pink';
console.log(arr1);
</script>
</head>
<body>
</body>
</html>
数组中存放1-10
var arr = [];
for (var i = 0 ; i <=100 ; i++){
// arr = i; 不要直接给数组赋值,否则以前元素全都没了
arr[i] = i + 1;
}
console.log(arr);
筛选数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
var newArr = [];
var j = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
newArr[j] = arr[i];
j++
}
}
console.log(newArr);
</script>
</head>
<body>
</body>
</html>
方法2
//方法二
var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
var newArr = [];
//newArr.length 刚开始是0
for (var i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
//新数组应该从0 开始,依次递增
newArr[newArr.length] = arr[i];
}
}
console.log(newArr);
删除数组指定元素------数组去重
要求:将数组 [2, 0, 6, 1, 77, 0, 52, 0, 25, 7]; 中的0去掉后。形成一个不包含0的新数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
newArr[newArr.length] = arr[i]
}
}
console.log(newArr);
</script>
</head>
<body>
</body>
</html>
翻转数组
要求:将数组 [‘red’,‘grenn’,‘blue’‘pink’] 的内容反过来存放
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = ['red', 'green', 'blue', 'pink'];
var newArr = [];
for (var i = arr.length; i > 0; i--) {
newArr[newArr.length] = arr[i - 1];
}
console.log(newArr);
</script>
</head>
<body>
</body>
</html>
|