js find函式胡實現 ES6語法

2021-09-29 15:51:47 字數 1592 閱讀 7661

find() 方法返回陣列中滿足提供的測試函式的第乙個元素的值 否則返回undefined

var    number =[5

,12,8

,130,44

];var found = number.

find

(function

(element));

console.

log(found)

;//結果12

查詢符合條件的指定元素

var   inventory =[,

,]; name = inventory.

find

(function

(element));

console.

info

(name)

;//得到結果

查詢元素,返回找到的值,找不到返回undefined

const   arr1 =[1

,2,3

,4,5

,6,7

,8,9

,10,11

];var ret1 = arr1.

find

((value, index, arr)

=>

)var ret2 = arr1.

find

((value, index, arr)

=>

) console.

log(

'%s'

, ret1)

console.

log(

'%s'

, ret2)

//執行結果

5 undefined

2. findindex 查詢元素返回索引

查詢元素 返回找到的index, 找不到返回 -1

var   ret3 = arr1.

findindex

((value, index, arr)

=>

)var ret4 = arr1.

findindex

((value, index, arr)

=>

) console.

log(

'%s'

, ret3)

console.

log(

'%s'

, ret4)

結果:4-

1

3. 實現方式
var users =[,

,,] array.prototype.

myfind

=function

(conditionfunc)

for(

var i =

0; i <

this

.length; i++)}

}var ret = users.

myfind

(function

(item, index)

) console.

log(ret)

es6箭頭函式

本例是在了解es6知識後在原來定義函式的基礎上進行理解var searchvalue 查詢匹配物件 var button var input var select button if searchvalue.input undefined button.click else 重新整理 tableli...

ES6 函式擴充套件

函式在js裡是相當重要的一部分了,es6裡也新增了一些函式方法,來看一下 test hello hello world test hello kill hello kill es6增加了函式引數預設值,可以直接在宣告引數的同時賦預設值,但是也可以後面重新賦值 test2 kill 括號內有引數x時,...

es6 箭頭函式

1.單引數 function cheng a 3 let cheng a 3 a a console.log cheng 9 2.多引數 function add a,b let add a,b a b 預設返回值 console.log add 3,9 3.無返回值 function add a,...