map filter reduce函式的使用

2022-08-26 13:03:09 字數 1058 閱讀 2422

//

1、篩選出大於30的數。

const array = [10, 20, 30, 40, 50, 60, 70, 80]

//普通寫法

//let newarray = array.filter(function (k) )

//es6寫法

let newarray = array.filter((k) => k > 30)

console.log(newarray)

//

2、把陣列元素乘2

//把陣列中的每個元素作為變數傳進去

let newarray2 = newarray.map(function

(n) )

console.log(newarray2)

//

3、把陣列中的元素相加

let newarray3 = array.reduce(function

(previousvalue, n) ,0)

console.log(newarray3)

1、篩選出大於30的數。

2、把陣列元素乘2

3、把陣列元素彙總

//

鏈式寫法

let newarray22 = array.filter(function

(k) ).map(

function

(k) ).reduce(

function

(previousvalue, k) )

console.log(newarray22)

//

箭頭函式寫法

let newarray333 = array.filter((k) => k > 30).map((k) => k * 2).reduce((previousvalue, k) => previousvalue +k)

console.log(newarray333)

map,filter,reduce函式介紹

map,filter,reduce都是python自帶的內建函式,都是通過某個函式對序列進行計算而得出某種結果。需注意在python3中,reduce函式被封裝在functools類下。下面來分別介紹一下這三個函式的具體用法 map會用傳入的函式對序列中的每個值做對映。比如序列為 x1,x2,x3 ...

Sql Server REPLACE函式的使用

replace 用第三個表示式替換第乙個字串表示式中出現的所有第二個給定字串表示式。語法replace string replace1 string replace2 string replace3 引數 string replace1 待搜尋的字串表示式。string replace1 可以是字元...

Sql Server REPLACE函式的使用

replace 更新替換資料庫某個欄位的的部分的值,比如替換資料庫中url欄位的網域名稱,語法 replace cloum string old string old 引數 cloum 待替換欄位的字串表示式。string old 待查詢的字串表示式。string old 替換用的字串表示式。示列 ...