标签:利用 包含 计算 类型 构造函数 数据 超出 div 函数
ECMAScript 数组可以保存任何类型的数据。数组最多包含 4294967295 个值
创建数组
1. 使用 Array 构造函数
1 <script> 2 //创建数组 arr1 3 var arr1 = new Array(); 4 //创建数组 arr2 , 数组length属性值为20 5 var arr2 = new Array(20); 6 //创建数组 arr3, 数组包含 3 个字符串值 7 var arr3 = new Array("red","blue","pink"); 8 9 //可以省略 new 操作符 10 var arr4 = Array(); 11 var arr5 = Array(20); 12 var arr6 = new Array("red","blue","pink"); 13 </script>
2. 使用数组字面量表示法
1 <script> 2 //创建空数组 arr1 3 var arr1 = []; 4 //创建数组 arr2, 数组包含 3 个字符串值 5 var arr2 = ["red","blue","pink"]; 6 </script>
不要像下面一样创建!
1 <script> 2 var arr1 = [1,2,]; 3 var arr2 = [,,,,,]; 4 5 console.log(arr1); //输出:(2) [1, 2] 6 console.log(arr1.length); //输出:2 7 console.log(arr2); //输出:(5) [empty × 5] 8 console.log(arr2.length); //输出:5 9 </script>
读取和设置数组的值
1 <script> 2 var arr1 = ["a","b","c"]; 3 //读取第 1 项 4 console.log(arr1[0]); //输出:a 5 //修改第 2 项 6 arr1[1] = "1"; 7 //新增第 3 项 8 arr1[3] = "3"; 9 console.log(arr1); //输出:(4) ["a", "1", "c", "3"] 10 //数组长度 11 console.log(arr1.length); //输出:4 12 //设置属性,从数组末尾移除项 13 arr1.length = 2; 14 console.log(arr1); //输出:(2) ["a", "1"] 15 //设置属性,从数组末尾新增项,新增的每一项都为 undefined 值 16 arr1.length = 4; 17 console.log(arr1); //输出:(4) ["a", "1", empty × 2] 18 console.log(arr1[2]); //输出:undefined 19 console.log(arr1[3]); //输出:undefined 20 //利用length属性添加新项 21 arr1[arr1.length] = "length"; 22 console.log(arr1); //输出:(5) ["a", "1", empty × 2, "length"] 23 arr1[arr1.length] = "length"; 24 console.log(arr1); //输出:(6) ["a", "1", empty × 2, "length", "length"] 25 //在超出当前数组大小的位置上添加值数组会重新计算数组长度 26 arr1[99] = 99; 27 console.log(arr1.length); //100 28 </script>
标签:利用 包含 计算 类型 构造函数 数据 超出 div 函数
原文地址:https://www.cnblogs.com/xiaoxuStudy/p/12242852.html