js數組基礎

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            //計算給定數組arr中全部元素的總和
            function sum(arr){
                var result = 0;
                for(var i = 0; i<arr.length;i++){
                    result = result + arr[i];
                }
                console.log(result);
            }
            sum([1,2,3]);
            
            //合併數組arr1和數組arr2,不要直接修改數組,結果返回新的數組
            function concat(arr1,arr2,arr3){
                var arr4 = arr1.concat(arr2,arr3);
                console.log(arr4);
            }
            concat(["s"],["a","b"],[1,3])
            //刪除數組arr第一個元素,不要直接修改數組arr,結果返回新的數組
            function deletes(arr){
                var arr2 = arr.slice(0);
                arr2.shift();
                console.log(arr2);
            }
            deletes(["s","d","c"])
            //在數組arr開頭添加元素item,不直接修改數值arr
            function preAddItem(arr,item){
                var arr2 = arr.slice(0);
                arr2.unshift(item)
                console.log(arr2);
            }
            
            preAddItem(["a","b","c"],"a");
            //移除數組arr中的全部值與item相等的元素,直接在給定數組上操做,並將結果返回
            function removeItem(arr,item){
                for(var i=0;i<arr.length;i++){
                    if(arr[i]==item){
                        arr.splice(i,1);
                        i--;
                    }
                }
                console.log(arr);
            }
            removeItem(["a","b","c","a"],"a");
            
            //移除數組的第三個元素,並在數組第三個位置添加新元素:(替換元素,把c替換成e,f)
            //2表明從刪除數組元素的開始位置(從何處添加/刪除元素。),1表明刪除數組是的元素個數,"e""f"表明添加的元素;
            //array.splice(index,num),num能夠爲0,即刪除0個元素;此方法改變的爲原數組內容
            function removeAdd(){
                var arr = ["a", "b", "c", "d"];
                var arrRemove = arr.splice(2,1,"e","f");
                console.log(arr);
                console.log(arrRemove);//輸出刪除的元素內容,即"c";
            }
            removeAdd();
            //找出元素item在給定數組arr中的位置
            function indexOf(arr,item){
                for(var i = 0;i<arr.length;i++){
                    if(arr[i] == item){
                        console.log(i);
                    }
                }
            }
            indexOf(["a","b","c"],"b");
            //用數組元素組成字符串
            function arrStr(arr){
                var arr2 = arr.join();
                console.log(arr2)
            }
            arrStr(["a","b"]);
            //刪除數組最後一個元素
            function removeArrEndItem(){
                arr = ["a","b","c"];
                var arr2 = arr.pop();
                console.log(arr);//在原數組上操做
                console.log(arr2);//刪除的元素
            }
            removeArrEndItem();
            //在數組末尾添加新元素
            function addEndItem(){
                var arr = ["a","b","c"];
                var arr2 = arr.push();
                console.log(arr);//在原數組上操做
                console.log(arr2);//數組arr的長度
            }
            addEndItem();
            //數組排序
            function sort(){
                var arr = ["c","b","a"];
                arr.sort();
                console.log(arr);
                
                var points = [40,100,1,5,25,10];
                points.sort(function(a,b){return a-b});
                console.log(points);
                
                var points01 = [40,100,1,5,25,10];
                points01.sort(function(a,b){return b-a});
                console.log(points01);
            }
            sort();
            //將數組元素轉換爲字符串輸出
            function str(){
                var fruits = ["Banana", "Orange", "Apple", "Mango"];
                var str = fruits.toString();
                console.log(str);
            }
            str();
        </script>
    </body>
</html>

 運行結果:javascript

相關文章
相關標籤/搜索