国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浠水县| 宣威市| 嘉祥县| 贡山| 武汉市| 民权县| 个旧市| 武夷山市| 定安县| 海口市| 阿尔山市| 沈阳市| 宜良县| 合水县| 锦屏县| 疏勒县| 宜州市| 成武县| 合山市| 钟祥市| 韶关市| 平潭县| 普洱| 镇安县| 青神县| 平度市| 壶关县| 郁南县| 河西区| 冷水江市| 化州市| 和硕县| 江口县| 宣汉县| 乌拉特中旗| 呼玛县| 日喀则市| 修水县| 聊城市| 股票| 苍南县|