(编辑:jimmy 日期: 2024/11/16 浏览:2)
Js中的数组是一种特殊的对象,用来表示偏移量的索引是该对象的属性。
具体代码如下所示:
// =================== 求最大值===================================== <script> var arr = [10,35,765,21345,678,89]; var max = arr [0]; for (var i=0;i< arr.length;i++) { if (max<arr[i]){ max = arr [i]; } } console.log("最大值:" + max); </script> // =================== 求最小值===================================== <script> var arr = [10,35,765,21345,678,89]; var max = arr [0]; for (var i=0;i< arr.length;i++) { if (max>arr[i]){ max = arr [i]; } } console.log("最小值:" + max); </script> // ============================倒序遍历数组=============================== <script> var arr = [1,2,3,4,5,6,7,8,9]; for (var i= arr.length-1;i>=0;i--){ console.log(arr[i]); } </script> //===========数组拼接字符串===========第一种方法(从前面往后加)========= <script> var names = ["周杰伦","李连杰","林俊杰","张韶涵","田馥甄"]; var str = ""; for (var i = 0; i< names.length-1;i++){ str += names[i] + "|" } console.log(str+ names[names.length-1]); </script> //===========数组拼接字符串===========第二种方法(从后面往前加)========= <script> var names = ["周杰伦","李连杰","林俊杰","张韶涵","田馥甄"]; var str = ""; for (var i = 1; i< names.length;i++){ str += "|" + names[i] } console.log(names[0]+ str); </script> // ==============去掉数组中重复的0,把其他的数据放在一个数组中============ <script> var arr = [10,20,0,12,0,0,124,32]; var newArr = []; for (var i=0; i<arr.length; i++){ if (arr[i] != 0){ newArr[newArr.length] = arr[i]; } } console.log(newArr); </script> // =====================反转数组----------把数组中的数据位置调换============= <script> var array = [10,20,30,40,50,60]; for (var i=0 ;i<=array.length/2;i++){ var temp = array [i]; array[i] = array[array.length-1-i]; array[array.length-1-i] = temp; } console.log(array); </script> //===========数列求成绩,总成绩,平均分,最大最小值===============// <script> var proInput = parseInt(prompt("请输入计研17的班级人数")); var proScore = []; for (var i = 0; i< proInput; i++){ proScore[proScore.length] = parseInt(prompt("请输入第"+ (i+1) + "个同学的成绩")); } console.log(proScore); var sum =0; var aver = 0; var max = proScore[0]; var min = proScore[0]; for (var i = 0;i< proScore.length;i++){ sum += proScore[i]; aver = sum/proScore.length; if (max<proScore[i]){ max = proScore[i]; } if (min>proScore[i]){ min = proScore[i]; } } console.log("班级总成绩:"+ sum); console.log("平均值:"+ aver); console.log("最大值:"+ max); console.log("最小值:"+ min); </script>
总结
以上所述是小编给大家介绍的JS中数组实现代码(倒序遍历数组,数组连接字符串),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!