JavaScript数组的基本概括

javaScript数组:有序,可扩容,可缩容,存储类型不要求一致。数组中可同时存储不同类型的数据。

//创建数组的三种方法
        //1.常规方式
        var myAry = new Array();
        myAry[0] = "hello";
        myAry[1] = "good";
        myAry[2] = "morning";
        console.log("myAry:"+myAry.length);
        //2.简洁方式
        var myAry02 = new Array("a","b","c");
        console.log("myAry02:"+myAry02.length);
        //3.字面方式
        var myAry03 = ["console","write","alert"];
        console.log(myAry03.length);
//数组中存储不同类型的数据
        var type = [123,"hello",3.14,true,undefined,NaN,null];
        //获取数组的长度,正确结果为7
        console.log(type.length);
//弹栈,压栈,扩容,缩容
        //压栈,添加元素
        myAry03.push(document);
        console.log(myAry03.length);
        //弹栈,移除元素
        var temp = myAry03.pop();
        console.log(temp);
        console.log(myAry03.length);
        //扩容,通过规定长度直接扩容
        myAry03.length= 10;
        console.log(myAry03[6]);
        //缩容
        myAry03.length= 3;
        console.log(myAry03.length);
/弹栈,压栈,扩容,缩容
        //压栈,添加元素
        myAry03.push(document);
        console.log(myAry03.length);
        //弹栈,移除元素
        var temp = myAry03.pop();
        console.log(temp);
        console.log(myAry03.length);
        //扩容,通过规定长度直接扩容
        myAry03.length= 10;
        console.log(myAry03[6]);
        //缩容
        myAry03.length= 3;
        console.log(myAry03.length);

        //三种遍历方式
        //1.普通for
        for(var i=0;i

        //创建空数组
        var arrays = [];
        //常量元素
        var a = 100;
        //添加进arrays
        arrays.push(a);
        //字符串元素
        var b = "100";
        //添加进arrays
        arrays.push(b);
        //boolean元素
        var c = true;
        //添加进arrays
        arrays.push(c);
        //字符串元素
        var d = "hello";
        //添加进arrays
        arrays.push(d);
        //数组元素
        var arr = [1,2];
        //添加进arrays
        arrays.push(arr);
        //函数元素
        var fun = function(){} ;
        //添加进arrays
        arrays.push(fun);
        //对象元素
        var obj = {};
        //添加进arrays
        arrays.push(obj);
        //未定义元素
        var un = undefined;
        //添加进arrays
        arrays.push(un);
        //NaN元素
        var na = NaN;
        //添加进arrays
        arrays.push(na);
        //遍历,判断类型
        for(var i in arrays){
            console.log(arrays[i]+"--"+typeof arrays[i]);
        }
        /*
        100--number
         100--string
         true--boolean
         hello--string
         1,2--object
         function(){}--function
         [object Object]--object
         undefined--undefined
         NaN--number
         */
    

你可能感兴趣的:(JavaScript数组的基本概括)