1.数组的基础应用
<script>
//定义数组,
var arr = [1,2,3];
//获取数组中的第二项,
var str = arr[1];
//给数组中的第三项赋值、
arr[2]= 10;
//获取数组的长度、
// console.log(arr.length);
// 正向遍历数组
for(var i = 0;i<arr.length;i++){
arr[i];
console.log(arr[i])
}
// 反向遍历数组、
for(var i = arr.length-1; i>=0;i-- ){
arr[i];
console.log(arr[i])
}
</script>
Snip20170817_5.png
2.字符串分割
//将字符串数组用\或者其他符号分隔
var array = ["aa","bb","cc","dd","ee","ff"];
var str = array[0];
var seperatpr = "|";
for(var i = 1;i<array.length;i++){
str += seperatpr + array[i];
}
console.log(str); //aa|bb|cc|dd|ee|ff
3.将数组中的某项值去除,生成新的数组
<script>
var array1 = [1,2,3,0,4,0,5,0,0,6,0,7,0,8,9]
var newArray =[];
for(var i = 0;i < array1.length; i++){
if (array1[i]!== 0){
newArray[newArray.length]= array1[i];
}
}
console.log(newArray);//Array [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
</script>
4.获取数组中每一个字符的长度,存储到一个新的数组中去
<script>
var array2 = ["asd","","123e","qwer"];
var NewArray = [];
for(var i = 0 ;i < array2.length;i++){
NewArray[NewArray.length] = array2[i].length;
}
console.log(NewArray); //Array [ 3, 0, 4, 4 ]
</script>
网友评论