jquery陣列 篩選陣列元素

2022-08-10 17:36:14 字數 1378 閱讀 4759

html:

<

h3>原始顯示

h3>

<

div

id="show5"

>

div>

<

h3>應用grep()方法後(1)

h3>

<

div

id="show6"

>

div>

<

h3>應用grep()方法後(2)

h3>

<

div

id="show7"

>

div>

jquery:

var animals = ['dog','cat','tiger','pig','bird'];

$('#show5').html(animals.join('

<

br/>

'));

//篩選出陣列元素長度小於5,並且索引大於2的

animals = $.grep(animals, function(n,i));

$('#show6').html(animals.join('

<

br/>

'));

//篩選出經過上面篩選之後的陣列裡的元素含有乙個p或者多個p的元素

animals = $.grep(animals, function(n));

$('#show7').html(animals.join('

<

br/>'));

顯示結果:

原始顯示

dogcat

tiger

pigbird

應用grep()方法後(1)

pigbird

應用grep()方法後(2)

pig

知識點:

1,grep()方法,此方法分析陣列的所有元素,為每個元素分別呼叫**函式。在**函式中編寫條件語句,把不想要的元素過濾掉,也就是說,**函式只返回被過濾陣列中我們想要的值。

jquery.grep( array, function(elementofarray, indexinarray), [ invert ] )

array原始陣列,用來被過濾。

function(elementofarray, indexinarray)**函式,執行篩選任務。該函式有兩個引數,第乙個參是陣列元素,第二個引數是索引。

invert預設false,沒有任何效果。如果設定為true,返回結果是與條件語句中不匹配的值。

2,match()正則匹配。

jQuery 元素篩選

eq 獲取給定索引的元素 功能跟 eq 一樣 first 獲取第乙個元素 功能跟 first 一樣 last 獲取最後乙個元素 功能跟 last 一樣 filter exp 留下匹配的元素 is exp 判斷是否匹配給定的選擇器,只要有乙個匹配就返回,true has exp 返回包含有匹配選擇器的...

IOS 篩選陣列內的元素

需要去掉的元素陣列 nsmutablearray filteredarray nsmutablearray alloc initwithobjects 1 4 nil 需要被篩選的陣列 nsmutablearray dataarray nsmutablearray alloc initwithobj...

jquery篩選元素函式

jquery篩選元素函式 1.過濾 匹配第二個p元素 button click function 匹配第乙個li元素 button click function 匹配最後乙個元素 button click function 檢查匹配的元素是否含有指定的類 button click function ...