标签:for ice 合并 js数组 设置 blog lin var array
一.定义数组:
方法1
var myCars=new Array(); myCars[0]="Saab"; myCars[1]="Volvo"; myCars[2]="BMW";
方法2:
var myCars=new Array("Saab","Volvo","BMW");
方法3.
var myCars=["Saab","Volvo","BMW"];
二.数组修改 使用for循环遍历
①数组中 指定值得位置
var x=myCars.length //数组长度 y=myCars.indexOf("Volvo") //某个值得位置
②合并数组 concat()
var hege = ["Cecilie", "Lone"]; var stale = ["Emil", "Tobias", "Linus"]; var children = hege.concat(stale);
③数组转化为字符串 tostring() 或者 join()【join可以随意设置间隔 字符 tostring 不行】
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var t =fruits.join("--");
结果:Banana--Orange--Apple--Mango
④:slice()//原来数组并没有改变
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1,3);//1为索引3为长度
结果:Orange,Lemon
⑤:splice()//直接改变原来数组
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2,0,"Lemon","Kiwi"); 直接改变原来数组
结果:Banana,Orange,Lemon,Kiwi,Apple,Mango
标签:for ice 合并 js数组 设置 blog lin var array
原文地址:http://www.cnblogs.com/ordinaryk/p/6390908.html