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

634 ℃

下面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,我们会在看到邮件的第一时间内为您处理!

浅谈php的索引数组和关联数组
帝国cms后台生成更新后前台页面显示空白
帝国CMS技巧之下载地址可以使用直接地址不用跳
DEDE集成百度搜索谷歌搜索站内全站搜索站内按栏
js数组是NaN如何判断