js数组
1.一维数组
设置数组求和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script language="JavaScript" type="text/jscript"> var weight=[1.3,2.3,3,4,5,3.21321]; var sum=0; //数组的遍历 for(var i=0;i<weight.length;i++){ sum+=weight[i]; document.write(weight[i]+" "); } document.writeln("总体重是"+sum+" "+"平均体重"+(sum/weight.length)+"<br/>"); document.write("体重保留为"+(sum/weight.length).toFixed(3)); </script> </head> <body> </body> </html>
数组的元素可以为任意元素,元素的值可以为任意的类型,
数组在内存中存在形式,数组为引用类型,
2.数组细节
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script language="JavaScript" type="text/jscript"> var weight = [1,2,3,4,5,6]; var sum = 0; function abc(arr){ weight[0]=9999; } //数组的遍历 abc(weight); for(var i = 0; i < weight.length; i++) { document.write(weight[i] + " "); } </script> </head> <body> </body> </html>
js中的数组为引用传递
3.数组的引用基本用法
越界的话会输出undefined
不能访问没定义的元素
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script language="JavaScript" type="text/jscript"> var weight = [1, "hello", 2.5]; window.alert(weight[16]); </script> </head> <body> </body> </html>
4.js的数组可以动态的增长
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script language="JavaScript" type="text/jscript"> var weight = [1, "hello", 2.5]; window.alert(weight.length); weight[3] = 12; window.alert(weight.length); </script> </head> <body> </body> </html>
5.split函数的用法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script language="JavaScript" type="text/jscript"> var word = "hello world,大家好"; var str = word.split(","); for(var i = 0; i < str.length; i++) { document.write(str[i] + "<br/>"); } </script> </head> <body> </body> </html>
对字符串进行分割
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script language="JavaScript" type="text/jscript"> var word = "hello world,大家好"; var str = word.split(","); for(var i = 0; i < str.length; i++) { document.write(str[i] + "<br/>"); } var str1=word.split(""); document.write("一个一个的分组"+"<br/>"); for(var i = 0; i < str1.length; i++) { document.write(str1[i] + " "); } str1=word.split(",",3); document.write(str[0]); document.write(str1.length); </script> </head> <body> </body> </html>
6.遍历数组的方式:var key in arr
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script language="JavaScript" type="text/jscript"> var word = "hello world,大家好"; var str = word.split(","); for(var i = 0; i < str.length; i++) { document.write(str[i] + "<br/>"); } var str1=word.split(""); document.write("一个一个的分组"+"<br/>"); for(var i = 0; i < str1.length; i++) { document.write(str1[i] + " "); } str1=word.split(",",3); document.write(str[0]); document.write(str1.length); </script> </head> <body> </body> </html>