Use of filter and map in ES

1.filter

Used to filter an array.
Do not operate the current array, return a new array, and return true elements in the callback.

let array = [1, 2, 3, 4, 5].filter(function (item) {
    return item > 2;
})
console.log(array)

结果:[ 3, 4, 5 ]

2.map

Do not operate the current array, return a new array, what is returned in the callback, what is this item.

let array2 = [2, 3, 4].map(function (item) {
    return `<li>${item}</li>`     //``模板字符串,遇到变量用${ }取值
})
console.log(array2)

结果:[ '<li>2</li>', '<li>3</li>', '<li>4</li>' ]

Guess you like

Origin www.cnblogs.com/bqh10086/p/12694607.html