Vue学习笔记(二):ES6中常用函数

随手整理一下一些有用到的ES6函数

0. forEach和map方法

1、相同点:

1.都是循环遍历数组中的每一项。
2.每次执行匿名函数都支持三个参数,参数分别为item(当前每一项),index(索引值),arr(原数组)。
3.匿名函数中的this都是指向window。
4.只能遍历数组。

2、不同点:

1.forEach()方法没有返回值,而map()方法有返回值。 forEach适合于你并不打算改变数据的时候。
2.map()适用于你要改变数据值的时候。不仅仅在于它更快,而且返回一个新的数组,比如需要对数组的数据操作

1. filter() 方法

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素

注意:
filter() 不会对空数组进行检测
filter() 不会改变原始数组

//1. 根据条件过滤想要的数据
var  ages = [32, 33, 12, 40];
result = ages.filter(function(age){
      return age >= 18;
});
console.log(result);  //输出结果  [32, 33, 40]

//例2 根据关键字搜索
var  list = [
          { id: 1, name: '奔驰', ctime: new Date() },
          { id: 2, name: '宝马', ctime: new Date() }
];
var  search = (keywords,list) => { 
     return list.filter(item => {
          if (item.name.includes(keywords)) {
              return item;
          }
     });
};
//删除数组中ID的数
search('宝马',  list);
console.log(list);  //  [{ id: 2, name: '宝马', ctime: new Date() }]

2. includes() 方法

includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回true,否则false。

语法:
参数两个,第一个参数是要查找的元素值 第二参数是可选的,是从该索引开始查找,默认为0。

// 例1
[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
// 例2
let  site = ['runoob', 'google', 'taobao'];
site.includes('runoob');  //true
site.includes('baidu');   //false

3.findIndex()

findIndex()方法返回(传入函数)符合条件的数组第一个元素位置
findIndex()方法为数组中的每个元素都调用一次函数执行:

当数组中的元素在传入闭包函数返回true是,findIndex()返回符合条件的元素的索引位置,之后的值不会再调用执行函数
如果没有符合条件的元素返回 -1
注意: findIndex()对于空数组,函数是不会执行的。
注意:findIndex()并没有改变数组的原始值

var  list = [
          { id: 1, name: '奔驰', ctime: new Date() },
          { id: 2, name: '宝马', ctime: new Date() }
];
var  del = (id,list) => { 
     var index = list.findIndex(item => {
        if(item.id == id) {
             return true;
        }
     });
     //删除数组中ID的数
     list.splice(index, 1);
};
del(1, list);
console.log(list);  //  [{ id: 2, name: '宝马', ctime: new Date() }]
 //不为空,拿到下标
 let index = this.list.findIndex(v => v.title == this.name);

具体代码参考:

https://www.runoob.com/jsref/jsref-obj-array.html

4.reduce()

reduce() 方法对数组中的每个元素执行一个由我们提供的reducer函数,且该函数为升序执行,并将其结果汇总为单个返回值

reduce的作用 : 数组累加器方法
reduce场景: 数组元素求和 、求数组元素最大值
reduce语法:数组名.reduce( ( 上一次值 , 当前值 , 当前下标 ) =>{ } , 初始值 )

//基本数组求和
var total = [ 0, 1, 2, 3 ].reduce(
  ( acc, cur ) => acc + cur,
  0
);
// total  6

具体代码参考:

https://blog.csdn.net/m0_65335111/article/details/126111819

https://blog.csdn.net/d_rui/article/details/124596612

5.every()

some表示只有一个满足条件就返回true,every则表示只有全部满足条件才会返回true。

作用 : 判断数组中 是否所有的 元素都满足条件
应用场景: 开关思想(购物车全选框)
every方法的返回值:
* true: 所有的元素 都符合条件
* false: 有元素 不符合条件

let arr = [20 , 50 , 60 , 88 , 25]

let res = arr.every(item => item > 20)

具体代码参考:

https://blog.csdn.net/m0_65335111/article/details/126111819

https://blog.csdn.net/d_rui/article/details/124596612

6. some() 方法

some() 方法用于检测数组中的元素是否满足指定条件(函数提供)
some() 方法会依次执行数组的每个元素

如果有一个元素满足条件,则表达式返回true,剩余的元素不会在执行检测
如果没有满足条件的元素,则返回false
注意:some() 不会对空数组进行检测
注意:some() 不会改变原始数组

var  list = [
          { id: 1, name: '奔驰', ctime: new Date() },
          { id: 2, name: '宝马', ctime: new Date() }
];
var  del = (id,list) => { 
     list.some((item, i) => {
          if (item.id == id) {
               this.list.splice(i, 1);
          }
     });
};
//删除数组中ID的数
del(1,  list);
console.log(list);  //  [{ id: 2, name: '宝马', ctime: new Date() }]
|| 版权声明
作者:云言
链接:https://yyink.cn/archives/291.html
声明:如无特别声明本文即为原创文章仅代表个人观点,版权归《云言博客》所有,欢迎转载,转载请保留原文链接。
THE END
分享
二维码
海报
Vue学习笔记(二):ES6中常用函数
随手整理一下一些有用到的ES6函数 0. forEach和map方法 1、相同点: 1.都是循环遍历数组中的每一项。 2.每次执行匿名函数都支持三个参数,参数分别为item(当前……
<<上一篇
下一篇>>