ES6学习之数组扩展
扩展运算符(...将数组分割为用逗号分割的参数序列)
console.log(...[1,2,3]) //1 2 3
可替换数组的apply写法:
function test(x,y,z){
console.log(x,y,z)
}
var arg = [1,2,3];
test.apply(null,arg) ////1 2 3
test(...arg) //1 2 3
扩展运算符的应用
- 复制数组
const a = [1, 2];
//复制数组a
const b = [...a] //方法1
const [...b] = a; //方法2
- 合并数组
const a = [1, 2];
const b = [3, 4];
const c = [...a, ...b]; //[ 1, 2, 3, 4 ]
- 解构赋值连用(与解构赋值连用,生成数组)
const [first, ...rest] = [1, 2, 3, 4, 5, 6];
console.log(first); //
console.log(rest); //[ 2, 3, 4, 5, 6 ]
- 字符串转数组
const a = [..."hello"];
console.log(a) //[ 'h', 'e', 'l', 'l', 'o' ]
- 实现了Iterator接口的对象转数组(只要具有 Iterator 接口的对象,都可以使用扩展运算符转为真正的数组)
let nodeList = document.querySelectorAll('div');
let array = [...nodeList];
- Map 和 Set 结构,Generator 函数
let map = new Map(
[
[1, "hello"],
[2, "ES6"],
[3, "test"]
]
)
let arr1 = [...map.keys()]; //[ 1, 2, 3 ]
let arr2 = [...map.values()]; //[ 'hello', 'ES6', 'test' ]
let arr3 = [...map.entries()]; //[ [ 1, 'hello' ], [ 2, 'ES6' ], [ 3, 'test' ] ]
Array.from()(将类似数组的对象和可遍历的对象转化为数组,只要具有length属性都能转)
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
};
let arr = Array.from(arrayLike); //[ 'a', 'b', 'c' ];
let str = "hello";
let arr2 = Array.from(str); //[ 'h', 'e', 'l', 'l', 'o' ]
Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组
let a = Array.from([1, 2, 3], x => x * x); //[ 1, 4, 9 ]
Array.of()(用于将一组值转化为数组,基本可以代替Array()和new Array())
let a0 = Array(0) //[]
let a1 = Array.of(0); //[0] let b0 = Array.of(1, 2, 3) //[ 1, 2, 3 ]
let b1 = Array(1, 2, 3) //[ 1, 2, 3 ]
Array.prototype.copyWithin(target, start = 0, end = this.length);
//接受三个参数
//target(必需):从该位置开始替换数据
//start(可选):从该位置开始读取数据,默认为0。如果为负值,表示倒数。
//end(可选):到该位置前停止读取数据,默认等于数组长度。如果为负值,表示倒数。
[1, 2, 3, 4, 5].copyWithin(0, 3)
// [4, 5, 3, 4, 5]
find()和findIndex()
- find()(数组中寻找成员,返回找到的第一个成员,否则返回undefined)
[1, 4, -5, 10].find((n) => n < 0) // -5
- findIndex() (数组中寻找成员,范围找到的该成员开始的第一个位置,否则返回-1)
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return value > 9;
}) //
fill()(使用给定值,填充一个数组)
['a', 'b', 'c'].fill(7) // [7, 7, 7]
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
//
//
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
includes()(数组中寻找数组成员,找到范围true,否则返回false)
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false
[1, 2, NaN].includes(NaN) // true
接收第二个参数,表示开的的位置
[1,2,3].includes(2,2) //false
ES6学习之数组扩展的更多相关文章
- es6基础(6)--数组扩展
//数组扩展 { let arr=Array.of(3,4,6,7,9,11);//可以是空 console.log('arr=',arr);//[3,4,6,7,9,11] } { //Array. ...
- ES6 学习6 数组的扩展
本章学习要点: 扩展运算符 Array.from() Array.of() 数组实例的 copyWithin() 数组实例的 find() 和 findIndex() 数组实例的 fill() 数组实 ...
- ES6学习之对象扩展
简介表示法(直接写入变量和函数,作为对象的属性和方法) let x = "test" let obj={ x, //属性名为变量名,属性值为变量值 y(){console.log( ...
- ES6学习之函数扩展
函数默认参数 function test(x = 1, y = 2) { return x + y } test(5, 6) test() 若默认参数在必须参数之前,要想取得默认参数,只有当传入的值为 ...
- es6核心特性-数组扩展
1. Array.from() : 将伪数组对象或可遍历对象转换为真数组 如果一个对象的所有键名都是正整数或零,并且有length属性,那么这个对象就很像数组,称为伪数组.典型的伪数组有函数的argu ...
- es6 语法 (数组扩展)
{ let arr = Array.of(3, 4, 7, 9, 11); console.log('arr', arr); //[3,4,7,9,11] let empty = Array.of() ...
- es6 学习四 数组的学习
1. Array.from() 语法: Array.from(arrayLike[, mapFn[, thisArg]]) arrayLike 类数组对象 mapFn 如果指定了该参数,新数组中的每个 ...
- ES6学习之数值扩展
二进制和八进制表示法(二进制用前缀0b(或0B)表示,八进制用前缀0o(或0O)表示) Number('0b111') Number('0o10') Number.isFinite()(判断一个值是否 ...
- ES6学习之正则扩展
RegExp正则函数 var reg = new RegExp("abc","igm"); //等价于 var reg = new RegExp(/abc/ig ...
随机推荐
- FPGA低温不能启动分析
FPGA低温不能启动分析 现象描写叙述:在给medium板光端机做低温试验时,分别给发送版.接收板断电又一次启动,发现有的板子在-40°能够启动,而有些板子在-20°都不能启动.须要升高温度到0°以上 ...
- Jquery实现loading效果
需要引入jquery和bootstrap相关包,然后把下面的代码复制进去就可以了: <div class="modal fade" id="loadingModal ...
- STM32 ~ 串口DMA通道查找
STM32F4XX: /**************************************************************************************** ...
- IOS NSDate 调整当前时间戳为明天
这个可以根据需要调整 在day month hour minute second 等都行 以下是以当前时间戳为基础,调整时间为明天的零点零时零分零秒 可以根据需要 写成毫秒的 +(NSStri ...
- iOS 8以后 定位手动授权问题
ios8以后 都是手动授权定位权限 不过不处理这块 在ios8以后的系统就会默认永不授权 即关闭了定位权限 处理办法如下 .导入框架头文件 #import <CoreLocation/CoreL ...
- mini2440移植uboot 2011.03(上)
参考博文: <u-boot-2011.03在mini2440/micro2440上的移植> 本来我想移植最新版的uboot,但是移植却不太成功,所以先模仿他人的例子重新执行一遍,对uboo ...
- dedecms常用标签
下面总结了58种常见的标签调用,包括关键描述调用.指定调用栏目.列表文章调用.频道栏目调用.当前栏目名称.栏目导航调用.模板路径调用.网站标题调用.友情链接调用.网站版权调用.网站备案调用.当前位置调 ...
- Docker 镜像篇
镜像是 Docker 容器的基石,容器是镜像的运行实例,有了镜像才能启动容器. docker两个跟镜像有关的命令: hello-world - 最小的镜像 hello-world 是 Docker 官 ...
- NetBeans字体设置
01.找到自己java字体目录.我的目录是[C:\Program Files\Java\jdk1.7.0_21\jre\lib] 02.复制fontconfig.properties.src, 重命名 ...
- 《机器学习实战》学习笔记第十四章 —— 利用SVD简化数据
相关博客: 吴恩达机器学习笔记(八) —— 降维与主成分分析法(PCA) <机器学习实战>学习笔记第十三章 —— 利用PCA来简化数据 奇异值分解(SVD)原理与在降维中的应用 机器学习( ...