国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js常用數組操作方法簡明總結

2019-11-20 14:26:29
字體:
來源:轉載
供稿:網友
//javascript 中的數組分割var colors = ["red","green","blue"];//alert(colors.toString());alert(colors.join("|")); //返回結果是red|green|bluevar colors = ["red","green","blue",null];alert(colors.join("|"));//red|green|blue|//注意當數組里面有值是null或者是undefined的時候 返回的結果是以空的字符串表示的-------------------------------------------//數組刪除和添加var colors = ["red","green","blue"];//alert(colors.toString());colors.push("white","test");//返回的結果是數組的長度alert(colors.join("|"));//結果是red|green|blue|white|test//往數組的開頭添加元素var colors = ["red","green","blue","test"];var item = colors.unshift("first");//數組的開頭添加一個元素alert(colors.join("|"));//返回最后的數組//刪除元素var colors = ["red","green","blue","test"];var item = colors.pop();//返回刪除的選項結果testalert(colors.join("|"));//返回最后的數組結果red|green|blue//刪除開頭元素var colors = ["red","green","blue","test"];var item = colors.shift();//刪除數組的第一個選項alert(colors.join("|"));//返回最后的數組-------------------------------------------------//數組順序事例//順序顛倒var colors = ["red","green","blue","test"];colors.reverse();alert(colors);//結果是:test,blue,green,red//數組排序var values = [0,1,5,10,7];values.sort(compare);alert(values);//document.writeln(values);} function compare(value1,value2){	if(value1<value2){		return 1 ;	}else if(value1>value2){		return -1 ;	}else return 0 ;} -----------------------------------------------------//向數組中添加數組 concat()方法var colors = ["color","red"];var colors2 = colors.concat(["ccc","bbbb"],'3333',['vvccxx',['oolll','lll']]);alert(colors2);//返回結果是:color,red,ccc,bbbb,3333,vvccxx,oolll,lll//slice()方法復制數組中的元素并不會破壞之前的元素var colors = ["color","red",'eeee','221111'];var colors2 = colors.slice(1);//從1開始進行復制alert(colors2);//結果是:red,eeee,221111var colors = ["color","red",'eeee','221111'];var colors2 = colors.slice(1,3);//從1開始進行復制到第3個位置結束alert(colors2);//結果是red,eeee---------------------------------------------------------------------//數組中刪除元素var a = [1,2,3,5,8];var r = a.splice(0,2); //刪除前2項alert(a);//結果是3,5,8var a = [1,2,3,5,8];var r = a.splice(1,1,100,200); //從第2個數開始刪除一項 然后插入100 200alert(a);//結果是1,100,200,3,5,8

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄迈县| 丰原市| 汨罗市| 凤阳县| 克什克腾旗| 辽中县| 卢湾区| 英山县| 将乐县| 成武县| 南木林县| 永清县| 古丈县| 浦北县| 岚皋县| 新宁县| 庐江县| 沧源| 拉萨市| 冷水江市| 车险| 陆丰市| 保德县| 武陟县| 静宁县| 农安县| 南昌市| 九江市| 敦化市| 鄂温| 高阳县| 台北县| 开封县| 通州市| 自治县| 布尔津县| 明水县| 吉水县| 朝阳县| 乾安县| 新疆|