javacript 數組操作

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>對象和數組</title>
</head>
<body>

</body>
</html>
<script type="text/javascript">
/*
var box = ['ogq',25,'student',new Date()];
alert(box);
alert(box.toString());
alert(box.valueOf());
alert(box.toLocaleString());        //本地區域格式字符串

var box = ['ogq',25,'student',new Date()];
alert(box.join('|'));               //join返回string 對原數組無影響

var box = ['ogq',25,'student'];
alert(box.push('中國','廣東'));         //返回最新長度
alert(box);

alert(box.pop());           //移除最後元素,並返回元素  後入先出
alert(box);

var box = ['ogq',25,'student'];
alert(box.shift());             //數組前段移除一個元素
alert(box);
alert(box.unshift('中國'));   
alert(box);                     //數組前段添加一個元素

var box = [1,2,3,4,5];
box.reverse();                  //逆向排序
alert(box);                     //引用

var box = [5,2,6,5,47,4];
box.sort();                     //沖銷到大排序,數字排序有問題
alert(box);

function compare(v1,v2) {       //數字排序
    if (v1<v2) {
        return  -1;
    }else if(v1>v2){
        return 1;
    }else{
        return 0;
    }
}
var box = [0,1,,10,5,15];
alert(box.sort(compare));

var box = ['ogq',35,'student'];
var box2 = box.concat('china');     //添加元素創建新數組,  原來的數組不變
alert(box2);
alert(box);

var box = ['ogq',35,'student'];
var box2 = box.slice(1);            //獲取制定位置元素,創建新數組
alert(box2);

var box = ['ogq',35,'student','China','廣東'];
var box2 = box.slice(1,4);          //從第一個個位置取到第三個位置
alert(box2);

var box = ['ogq',35,'student','China','廣東'];
var box2 = box.splice(2,3);         //從第二個位置取三個元素
alert(box2);                        //會刪除原數組元素

var box = ['ogq',35,'student','China','廣東'];
var box2 = box.splice(2,1,"韶關",'樂昌');   //刪除一個元素,並在通位置插入元素  2,0則表示不刪除,只插入
alert(box2);                                //替換通過原理
alert(box);
*/

</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章