欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript高級程序設(shè)計(jì) 讀書筆記之九 本地對象Array

 更新時(shí)間:2012年02月27日 23:50:56   作者:  
本地對象Array,數(shù)組等操作函數(shù)
創(chuàng)建Array對象
復(fù)制代碼 代碼如下:

//one
var aValues=new Array();

//two
var aValues=new Array(20);

//three
var aColors=new Array();
aColors[0]="red";
aColors[1]="green";
aColors[2]="blue";

//four
var aColors=new Array("red","green","blue");

//five
var aColors=["red","green","blue"];

join && split
join:連接字符串
復(fù)制代碼 代碼如下:

var aColors=["red","green","blue"];
alert(aColors.join(","));//outputs "red,green,blue"
alert(aColors.join("-spring-"));//outputs "red-spring-green-spring-blue"
alert(aColors.join("]["));//outputs "red][green][blue"

split:分拆字符串
復(fù)制代碼 代碼如下:

var sColors="red,green,blue";
var aColors=sColors.split(",");//outputs ["red", "green", "blue"]
var redColors=aColors[0].split("");//outputs ["r", "e", "d"]

concat && slice
concat:追加數(shù)組
復(fù)制代碼 代碼如下:

var aColors=["red","green","blue"];
var aColors2=aColors.concat("yellow","purple");
alert(aColors);//outputs ["red", "green", "blue"]
alert(aColors2);//outputs ["red", "green", "blue", "yellow", "purple"]

slice:返回具有特定項(xiàng)的新數(shù)組
復(fù)制代碼 代碼如下:

var aColors=["red","green","blue","yellow","purple"];
var aColors2=aColors.slice(1);//outputs ["green","blue","yellow","purple"]
var aColors3=aColors.slice(1,4);//outputs ["green","blue","yellow"]

push && pop
跟棧一樣,Array提供了push和pop方法,push方法用于在Array結(jié)尾添加一個或多個項(xiàng),pop用于刪除最后一個數(shù)組項(xiàng),返回它作為函數(shù)值
復(fù)制代碼 代碼如下:

var stack=new Array;
stack.push("red");
stack.push("green");
stack.push("blue");
alert(stack);//outputs ["red","green","blue"]
var vItem=stack.pop();
alert(vItem);//outputs ["blue"]
alert(stack);//otputs ["red","green"]

shift && unshift
shift:刪除數(shù)組中第一項(xiàng),將其作為函數(shù)返回值,unshift:把一個項(xiàng)放在數(shù)組的第一個位置,然后把余下的項(xiàng)向下移動一個位置
復(fù)制代碼 代碼如下:

var aColors=["red","green","blue"];
var vItem=aColors.shift();
alert(aColors);//outputs ["green","blue"]
alert(vItem);//outputs ["red"]
aColors.unshift("black");
alert(aColors);//outputs ["black","green","blue"]

reverse && sort
reverse:顛倒數(shù)組項(xiàng)的順序,sort:按數(shù)組項(xiàng)的值升序排列(首先要調(diào)用toString()方法,將所有值轉(zhuǎn)換成字符串)
復(fù)制代碼 代碼如下:

var aColors=["blue","green","red"];
aColors.reverse();
alert(aColors);//outputs ["red","green","blue"]
aColors.sort();
alert(aColors);//outputs ["blue","green","red"]

注意:
復(fù)制代碼 代碼如下:

var aColors=[3,32,2,5];
aColors.sort();
alert(aColors);//outputs [2,3,32,5]

這是因?yàn)閿?shù)字被轉(zhuǎn)換成字符串,然后按字符代碼進(jìn)行比較的。

splice
splice:把數(shù)據(jù)項(xiàng)插入數(shù)組的中部

1、用作刪除:只要聲明兩個參數(shù),第一個參數(shù)為要刪除的第一個項(xiàng)的位置,第二個參數(shù)為刪除項(xiàng)的個數(shù)
復(fù)制代碼 代碼如下:

var aColors=["red","green","blue","yellow"];
aColors.splice(0,2);
alert(aColors);//outputs ["blue", "yellow"]

2、用作插入:聲明三個或以上參數(shù)(第二個參數(shù)為0)就可以把數(shù)據(jù)插入指定位置,第一個參數(shù)為地始位置,第二個參數(shù)為0,第三個及以上參數(shù)為插入項(xiàng)
復(fù)制代碼 代碼如下:

var aColors=["red","green","blue","yellow"];
aColors.splice(2,0,"black","white");
alert(aColors);//outputs ["red","green","black","white","blue", "yellow"]

3、用作刪除并插入:聲明三個或以上參數(shù)(第二個參數(shù)為不0)就可以把數(shù)據(jù)插入指定位置,第一個參數(shù)為地始位置,第二個參數(shù)為要刪除的項(xiàng)的個數(shù),第三個及以上參數(shù)為插入項(xiàng)
復(fù)制代碼 代碼如下:

var aColors=["red","green","blue","yellow"];
aColors.splice(2,1,"black","white");
alert(aColors);//outputs ["red","green","black","white", "yellow"]

相關(guān)文章

  • 微信小程序仿淘寶熱搜詞在搜索框中輪播功能

    微信小程序仿淘寶熱搜詞在搜索框中輪播功能

    這篇文章主要介紹了微信小程序—仿淘寶熱搜詞在搜索框中輪播功能的相關(guān)資料,需要的朋友可以參考下
    2020-01-01
  • JS之判斷是否為對象或數(shù)組的幾種方式總結(jié)

    JS之判斷是否為對象或數(shù)組的幾種方式總結(jié)

    這篇文章主要介紹了JS之判斷是否為對象或數(shù)組的幾種方式總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • js+html+css實(shí)現(xiàn)手動輪播和自動輪播

    js+html+css實(shí)現(xiàn)手動輪播和自動輪播

    這篇文章主要為大家詳細(xì)介紹了js+html+css實(shí)現(xiàn)手動輪播和自動輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 讓js彈出窗口居前顯示的實(shí)現(xiàn)方法

    讓js彈出窗口居前顯示的實(shí)現(xiàn)方法

    一般來說,顯示在最前面的窗口都是因?yàn)榇翱诒患せ瞰@得焦點(diǎn),要使窗口永遠(yuǎn)顯示在最前面,可以人為的設(shè)置窗口在blur時(shí)立刻focus,或者用模式、無模式對話框?qū)崿F(xiàn)
    2013-07-07
  • 10個功能強(qiáng)大的JavaScript動畫庫分享

    10個功能強(qiáng)大的JavaScript動畫庫分享

    動畫,從人群中脫穎而出、吸引訪客注意力的絕佳方式,本文將給大家分享10 個功能強(qiáng)大的 JavaScript 動畫庫,有了這 10 個功能強(qiáng)大的 JavaScript 庫,創(chuàng)建動畫再簡單不過了,感興趣的同學(xué)可以參考閱讀
    2023-09-09
  • Echarts讀取動態(tài)數(shù)據(jù)完整代碼

    Echarts讀取動態(tài)數(shù)據(jù)完整代碼

    這篇文章主要給大家介紹了關(guān)于Echarts讀取動態(tài)數(shù)據(jù)的相關(guān)資料,使用Echarts畫圖時(shí),數(shù)據(jù)一般不是靜態(tài)寫死的,而是通過后端接口動態(tài)獲取的,需要的朋友可以參考下
    2023-10-10
  • JS將指定的某個字符全部轉(zhuǎn)換為其他字符實(shí)例代碼

    JS將指定的某個字符全部轉(zhuǎn)換為其他字符實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于JS如何將指定的某個字符全部轉(zhuǎn)換為其他字符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • JavaScript原生節(jié)點(diǎn)操作小結(jié)

    JavaScript原生節(jié)點(diǎn)操作小結(jié)

    本文主要介紹了JavaScript原生節(jié)點(diǎn)操作的相關(guān)知識。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • 用js實(shí)現(xiàn)用戶注冊功能

    用js實(shí)現(xiàn)用戶注冊功能

    這篇文章主要為大家詳細(xì)介紹了用js實(shí)現(xiàn)用戶注冊的簡潔版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論