es6语法中最常见的9个函数介绍

609 ℃

下面web建站给大家介绍一下es6语法中最常见的9个函数介绍,它们分别是forEachfiltermapconcatfindfindIndexincludessplitsubstr等。

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

filter实现数组过滤,es6语法过滤方法合集

ES6语法中有哪些简单的字符串转数组方法

ES6如何实现模块化编程

ES6语法种有哪些判断方法

vue语法中methods箭头函数的基本用法吗?

标签: concat, es6语法, filter, find, findIndex, forEach, includes, map, split, substr

上面是“es6语法中最常见的9个函数介绍”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_2423.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

网站优化中合适的关键词密度控制是多少?
Nginx配置什么原因导致php错误不提示
网站快照回档的原因是什么?
帝国cms在内容页模板里添加错误报告链接
jquery如何删除html属性值