下面web建站给大家介绍一下es6
语法中最常见的9个函数介绍,它们分别是forEach
、filter
、map
、concat
、find
、findIndex
、includes
、split
、substr
等。
forEach
let texts = ""; let arrs = ["Apple", "Orange", "Cherry", "Banana"]; arrs.forEach((item,index)=>{ texts += item + ',' ; }); console.log(texts) // Apple,Orange,Cherry,Banana,
filter
let numbers = [15, 2, 50, 55, 90, 5, 4, 9, 10]; console.log(numbers.filter(number => number % 2 == 1)); // [15, 55, 5, 9]
map
let arr = [1, 2, 3]; let newArr = arr.map(num => num * 2); console.log(newArr) // [2, 4, 6]
concat
let str1 = "你好,"; let str2 = "中国!"; let str3 = str1.concat(str2); console.log(str3) //你好,中国!
find
const fruits = [ { name: "apple", count: 10 }, { name: "banana", count: 18 }, { name: "mango", count: 3 } ]; const findMango = fruits.find(fruit =>fruit.name === "mango"); console.log(findMango) //{name: 'mango', count: 3}
findIndex
const fruits = [ { name: "apple", count: 10 }, { name: "banana", count: 18 }, { name: "mango", count: 3 } ]; const findMango = fruits.findIndex(fruit =>fruit.name === "mango"); console.log(findMango) //2
includes
const birds = ["Birds", "peacock", "Dove", "Sparrow"]; console.log(birds.includes("Dove")); //true
split
let text = "Hello wo name china"; console.log(text.split(" ", 3)); //['Hello', 'wo', 'name']
substr
const test = "Hello wo name china, who are you."; console.log(test.substr(0, 30)); //Hello wo name china, who are y
标签: concat, es6语法, filter, find, findIndex, forEach, includes, map, split, substr
上面是“es6语法中最常见的9个函数介绍”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_2423.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!