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

首頁 > 語言 > JavaScript > 正文

總結js常用數組的操作方法

2024-05-06 15:46:03
字體:
來源:轉載
供稿:網友
數組是就是數據的集合,在JavaScript開發中,數組中的數據有各種不同的類型,今天就跟著錯新技術頻道小編的步伐來總結js常用數組的操作方法吧!//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

大家可以按照錯新技術頻道小編的介紹才試試,如果操作過程中有任何的錯誤,可以在下方給小編留言,我們會盡快回復大家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 柏乡县| 西畴县| 都江堰市| 时尚| 广灵县| 鞍山市| 博客| 宁化县| 冕宁县| 临高县| 新和县| 通州市| 平阴县| 定远县| 辽阳市| 盐源县| 多伦县| 阿克苏市| 吉隆县| 临漳县| 会泽县| 太湖县| 潢川县| 旬阳县| 黄山市| 界首市| 东丽区| 深圳市| 和平县| 乐业县| 沅江市| 化州市| 高淳县| 长兴县| 兰州市| 临沧市| 宣化县| 页游| 新巴尔虎左旗| 彩票| 封开县|