1、Array 的扩展方法
1.1 扩展运算符(展开语法)
- 扩展运算符(…)可以将数组或者对象转为用逗号分隔的参数序列。
// 1. 扩展运算符(...)可以将数组或者对象转为用逗号分隔的参数序列
let ary = ['a', 'b', 'c'];
// ...ary; // 'a' , 'b', 'c'
console.log(...ary); // a b c
- 扩展运算符可以应用于合并数组。
// 2. 扩展运算符应用于合并数组
let ary1 = [1, 2, 3];
let ary2 = [4, 5, 6];
// ...ary1 // 1, 2, 3
// ...ary2 // 4, 5, 6
let ary3 = [...ary1, ...ary2];
console.log(ary3); // [1, 2, 3, 4, 5, 6]
// 合并数组的第二种方法
ary1.push(...ary2);
console.log(ary1); // [1, 2, 3, 4, 5, 6]
- 将类数组或可遍历对象转换为真正的数组。
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
// 3. 利用扩展运算符将伪数组转换为真正的数组
var divs = document.getElementsByTagName('div');
console.log(divs); // HTMLCollection(6) [div, div, div, div, div, div]
var ary4 = [...divs];
ary4.push('a');
console.log(ary4); // (7)[div, div, div, div, div, div, "a"]
1.2 构造函数方法:Array.from()
- 该方法可以将类数组或可遍历对象转换为真正的数组。
// 构造函数方法Array.from()
// 1. 将类(伪)数组或可遍历对象转换为真正的数组
var arrayLike = {
"0": "张三",
"1": "李四",
"2": "王五",
"length": 3
};
var ary = Array.from(arrayLike);
console.log(ary); // ["张三", "李四", "王五"]
- 该方法还可以接受第二个参数,作用类似于数组的 map 方法,用来对每个元素进行处理,将处理后的值放入返回的数组。
// 2. 方法还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组
var arrayLike2 = {
"0": "1",
"1": "2",
"length": 2
};
var ary2 = Array.from(arrayLike2, item => item * 2);
console.log(ary2); // [2, 4]
1.3 实例方法:find()
find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
find() 方法为数组中的每个元素都调用一次函数执行:
- 当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
- 如果没有符合条件的元素返回 undefined .
简单来说就是:find() 方法用于找出第一个符合条件的数组成员,如果没有找到返回 undefined。
注意:
- find() 对于空数组,函数是不会执行的。
- find() 并没有改变数组的原始值。
语法:
array.find(function(currentValue, index, arr), thisValue)
其中:
- currentValue:必需。表示当前元素
- index:可选。表示当前元素的索引
- arr:可选。表示当前元素所属的数组对象
- thisValue:可选。 传递给函数的值一般用 “this” 值。如果这个参数为空, “undefined” 会传递给 “this” 值。
// find() 方法用于找出第一个符合条件的数组成员,如果没有找到返回undefined
var ary = [{
id: 1,
name: '张三'
}, {
id: 2,
name: '李四'
}];
let target = ary.find(item => item.id == 1);
console.log(target); // {id: 1, name: "张三"}
let target1 = ary.find(item => item.id == 3);
console.log(target1); // undefined
1.4 实例方法:findIndex()
findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
findIndex() 方法为数组中的每个元素都调用一次函数执行:
- 当数组中的元素在测试条件时返回 true 时,findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
- 如果没有符合条件的元素返回 -1 。
简单来说就是,findIndex()用于找出第一个符合条件的数组成员的位置,如果没有找到返回 -1 。
注意:
- findIndex() 对于空数组,函数是不会执行的。
- findIndex() 并没有改变数组的原始值。
语法:
array.findIndex(function(currentValue, index, arr), thisValue)
其中:
- currentValue:必需。表示当前元素
- index:可选。表示当前元素的索引
- arr:可选。表示当前元素所属的数组对象
- thisValue:可选。 传递给函数的值一般用 “this” 值。如果这个参数为空, “undefined” 会传递给 “this” 值。
// findIndex()方法
// 用于找出第一个符合条件的数组成员的位置,如果没有找到返回-1
let ary = [10, 20, 50];
let index = ary.findIndex(item => item > 15);
console.log(index); // 1
1.5 实例方法:includes()
表示某个数组是否包含给定的值,返回布尔值。
// includes()方法
// 表示某个数组是否包含给定的值,返回布尔值
let ary = ['a', 'b', 'c'];
console.log(ary.includes('a')); // true
console.log(ary.includes('e')); // false
console.log(ary.includes('d')); // false
2、String 的扩展方法
2.1 模板字符串
ES6新增的创建字符串的方式,使用反引号定义。
- 模板字符串中可以解析变量。
// 模板字符串: ES6新增的创建字符串的方式,使用反引号(``)定义
let name = `张三`;
// 1. 模板字符串中可以解析变量
let sayHello = `Hello, 我的名字叫${name}`;
console.log(sayHello); // Hello, 我的名字叫张三
- 模板字符串中可以换行
// 2. 模板字符串中可以换行
let result = {
name: 'zhangsan',
age: 20
};
let html = `<div>
<span>${result.name}</span>
<span>${result.age}</span>
</div>`;
console.log(html);
// 输出结果为:
// <div>
// <span>zhangsan</span>
// <span>20</span>
// </div>
- 在模板字符串中可以调用函数。
// 3. 在模板字符串中可以调用函数
const fn = () => {
return '我是fn函数';
};
let html1 = `我是模板字符串 ${fn()}`;
console.log(html1); // 我是模板字符串 我是fn函数
2.2 实例方法:startsWith() 和 endsWith()
startsWith()
:表示参数字符串是否在原字符串的头部,返回布尔值 。endsWith()
:表示参数字符串是否在原字符串的尾部,返回布尔值。
// startsWith()方法和endsWith()方法
// startsWith():表示参数字符串是否在原字符串的头部,返回布尔值
// endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
let str = 'Hello ECMAScript 2015';
let r1 = str.startsWith('Hello');
console.log(r1); // true
let r2 = str.endsWith('2016');
console.log(r2); // false
2.3 实例方法:repeat()
repeat 方法表示将原字符串重复n次,返回一个新字符串。
// repeat()方法
// repeat()方法表示将原字符串重复n次,返回一个新字符串
console.log("y".repeat(5)); // yyyyy
console.log('hello'.repeat(2)); // hellohello
3、Set 数据结构
3.1 Set 数据结构
ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。
- Set 本身是一个构造函数,用来生成 Set 数据结构。
// 1. Set本身是一个构造函数,用来生成 Set 数据结构。
const s1 = new Set();
console.log(s1.size); // 0
- Set函数可以接受一个数组作为参数,用来初始化。
// 2. Set函数可以接受一个数组作为参数,用来初始化
const s2 = new Set(['a', 'b']);
console.log(s2); // {"a", "b"}
console.log(s2.size); // 2
// Set 成员的值都是唯一的,没有重复的值
const s3 = new Set(["a", "a", "b", "b"]);
console.log(s3); // {"a", "b"}
console.log(s3.size); // 2
const ary = [...s3];
console.log(ary); // ["a", "b"]
3.2 实例方法
- add(value):添加某个值,返回 Set 结构本身 。
- delete(value):删除某个值,返回一个布尔值,表示删除是否成功 。
- has(value):返回一个布尔值,表示该值是否为 Set 的成员 。
- clear():清除所有成员,没有返回值 。
// 3. 实例方法:
const s = new Set();
// (1) 向 set 结构中添加值,使用add方法
s.add(1).add(2).add(3);
console.log(s); // {1, 2, 3}
console.log(s.size); // 3
// (2) 从set结构中删除值 用到的方法是delete
const r1 = s.delete(2); // 删除 set 结构中的2值
console.log(s); // {1, 3}
console.log(s.size); // 2
console.log(r1); // true
// (3) 判断某一个值是否是set数据结构中的成员 使用has
const r2 = s.has(1) // 表示 set 结构中是否有1这个值 返回布尔值
console.log(r2); // true
// (4) 清空set数据结构中的值 使用clear方法
s.clear(); // 清除 set 结构中的所有值
console.log(s.size); // 0
- Set 结构的实例与数组一样,也拥有 forEach 方法,用于对每个成员执行某种操作,没有返回值
// 4. Set 结构的实例与数组一样,也拥有forEach方法,用于对每个成员执行某种操作,没有返回值
// 遍历set数据结构 从中取值
const s5 = new Set(['a', 'b', 'c']);
s5.forEach(value => {
console.log(value);
});