首页 > 代码库 > javascript基础之数组一

javascript基础之数组一

<script type="text/javascript">//求数组中最大的数var arr=[123,456,789,657,432,564];var arrMax= getMaxValue(arr);alert(arrMax);function getMaxValue(arrInt){    var max=arrInt[0];    for(var i=0;i<arr.length;i++)    {        if(arrInt[i] > max)        {                     max=arrInt[i];        }    }    return max;}</script>

结果:789

//数组转换为字符串
<script type="text/javascript">var names=["京客隆","问候语","时间的话","搜舞蹈","圣诞节快乐"]var named=getName(names);alert(named);function getName(strName){ var str=""; for(var i=0; i<strName.length;i++) { str=str+strName[i]+"|"; } return str.substring(0,str.length-1);}</script>

结果:京客隆|问候语|时间的话|搜舞蹈|圣诞节快乐

 

 //去掉id为1的对象组    <script type="text/javascript">        var modJson=[{id:"1",name:"wwee",addr:"3qw"},{id:"2",name:"user",addr:"kkk"},{id:"3",name:"we42",addr:"23ds"}];        for (var i = 0; i < modJson.length; i++) {                    if (modJson[i].id == 1) {                        modJson.splice($.inArray(modJson[i], modJson), 1);//去掉id为1的对象组                    }        }        console.info(modJson);    </script>
//数组_顺序翻转<script type="text/javascript">var names=["京客隆","问候语","时间的话","搜舞蹈","huahua"]getName(names);alert(names);function getName(strName){    var str;    for(var i=0; i<strName.length/2;i++)    {        str=strName[i];        strName[i]=strName[strName.length-1-i];        strName[strName.length-1-i]=str;    }    }</script>

结果为:huahua,搜舞蹈,时间的话,问候语,京客隆

 

//for和in的用法<script type="text/javascript">var mycars=new Array("any","mlik","yli");var mycars1=new Array("df","w3","fgf");for(var i=0;i<mycars.length;i++){document.write(mycars[i]+"<br/>");}for(x in mycars1){document.write(mycars1[x]+"<br/>")}document.write(mycars.concat(mycars1))//concat() 方法用于连接两个或多个数组。</script>

结果:

any
mlik
yli
df
w3
fgf
any,mlik,yli,df,w3,fgf      

 

<script>var caser=new Array("我是谁","你是谁","他是谁");//声明变量caser为数组类型for(var i=0;i<caser.length;i++){    document.write(caser[i]+"<br>");    }        var unn=new Array();    var j;    unn[0]="123";    unn[1]="456";    unn[2]="789";    for(j=0; j<unn.length;j++)    {        document.write(unn[j]);        }    </script>

 

javascript基础之数组一