扩展运算符(...将数组分割为用逗号分割的参数序列)

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 ]
copyWithin()(将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组。使用这个方法,会修改当前数组。)
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]
entries(),keys() 和 values()(keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历)
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学习之数组扩展的更多相关文章

  1. es6基础(6)--数组扩展

    //数组扩展 { let arr=Array.of(3,4,6,7,9,11);//可以是空 console.log('arr=',arr);//[3,4,6,7,9,11] } { //Array. ...

  2. ES6 学习6 数组的扩展

    本章学习要点: 扩展运算符 Array.from() Array.of() 数组实例的 copyWithin() 数组实例的 find() 和 findIndex() 数组实例的 fill() 数组实 ...

  3. ES6学习之对象扩展

    简介表示法(直接写入变量和函数,作为对象的属性和方法) let x = "test" let obj={ x, //属性名为变量名,属性值为变量值 y(){console.log( ...

  4. ES6学习之函数扩展

    函数默认参数 function test(x = 1, y = 2) { return x + y } test(5, 6) test() 若默认参数在必须参数之前,要想取得默认参数,只有当传入的值为 ...

  5. es6核心特性-数组扩展

    1. Array.from() : 将伪数组对象或可遍历对象转换为真数组 如果一个对象的所有键名都是正整数或零,并且有length属性,那么这个对象就很像数组,称为伪数组.典型的伪数组有函数的argu ...

  6. es6 语法 (数组扩展)

    { let arr = Array.of(3, 4, 7, 9, 11); console.log('arr', arr); //[3,4,7,9,11] let empty = Array.of() ...

  7. es6 学习四 数组的学习

    1. Array.from() 语法: Array.from(arrayLike[, mapFn[, thisArg]]) arrayLike 类数组对象 mapFn 如果指定了该参数,新数组中的每个 ...

  8. ES6学习之数值扩展

    二进制和八进制表示法(二进制用前缀0b(或0B)表示,八进制用前缀0o(或0O)表示) Number('0b111') Number('0o10') Number.isFinite()(判断一个值是否 ...

  9. ES6学习之正则扩展

    RegExp正则函数 var reg = new RegExp("abc","igm"); //等价于 var reg = new RegExp(/abc/ig ...

随机推荐

  1. 深入ConcurrentHashMap二

    深入ConcurrentHashMap一,已经介绍了主要的ConcurrentHashMap的结构,Segment组成,HashEntry的组成以及包含ConcurrentHashMap的创建. 这篇 ...

  2. ABAP操作EXCEL (号称超级版)

    [转自http://www.cnblogs.com/VerySky/articles/2170014.html] *------------------------------------------ ...

  3. python基础11 ---函数模块1

    函数模块 一.函数模块的作用(为什么要有函数模块) 1.函数模块可以减少代码量 2.函数模块方便阅读 3.函数模块维护性强二.函数模块的本质以及调用方法 1.函数模块的本质就是一个.py结尾的文件,该 ...

  4. pandas to_datetime()

    >>> import pandas as pd >>> i = pd.date_range() >>> df = pd.DataFrame(dic ...

  5. Python OOP(2)-static method,class method and instance method

    静态方法(Static Method): 一种简单函数,符合以下要求: 1.嵌套在类中. 2.没有self参数. 特点: 1.类调用.实例调用,静态方法都不会接受自动的self参数. 2.会记录所有实 ...

  6. Video Brightness Enhancement

    Tone Mapping原是摄影学中的一个术语,因为打印相片所能表现的亮度范围不足以表现现实世界中的亮度域,而如果简单的将真实世界的整个亮度域线性压缩到照片所能表现的亮度域内,则会在明暗两端同时丢失很 ...

  7. uCGUI 按键窗口切换机制

    前段时间在做一个窗口项目,这个项目菜单项过多,在管理起来比较麻烦.想做一个高效移植又方便的一个切换机制.后来在网上多方查找这方面资料,但是感觉比较少.后来自己整理出了这个结构,希望对后来朋友有所帮助. ...

  8. P4773 红鲤鱼与绿鲤鱼

    P4773 红鲤鱼与绿鲤鱼 暑假比赛的一个水题 总情况数:\(\dfrac{(a+b)!}{a!b!}\) 就是\(a+b\)条鲤鱼中选\(a\) or \(b\)的情况 反正我们会用完鲤鱼,则红鲤鱼 ...

  9. 十八 Django框架,生成二维码

    用Python来生成二维码,需要qrcode模块,qrcode模块依赖Image 模块,所以首先安装这两个模块 生成二维码保存图片在本地 import qrcode img = qrcode.make ...

  10. Hibernate映射--基本类映射和对象关系映射(转)

    原文地址:http://blog.csdn.net/lovesummerforever/article/details/20901011   尊重原创,请访问原网址 回想一些我们在没有学习ssh的时候 ...