JS中数组实现(倒序遍历数组,数组连接字符串)
Posted 风致﹏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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中数组实现(倒序遍历数组,数组连接字符串)的主要内容,如果未能解决你的问题,请参考以下文章