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 ...
随机推荐
- Oracle数据库获取uuid函数
Oracle新建系统表时,要求主键为32位uuid,推測Oracle肯定会提供相关的函数. 翻阅相关文档,果然发现Oracle提供的函数 sys_guid() 用于获取32位uuid,简单使用为 se ...
- Yii2 如何实现表单事件之 Ajax 提交
前言 Yii2 现在使用 JS 都必须要注册代码了. 要实现 Ajax 提交,有两种方法.一是直接在 ActiveForm 调用 beforeSubmit 参数,但是个人认为这样没有很好的把 JS 和 ...
- CodeForces - 580C Kefa and Park 【BFS】
题目链接 http://codeforces.com/problemset/problem/580/C 题意 根节点是 1 然后所有的叶子结点都是饭店 从根节点到叶子结点的路径上 如果存在 大于m 个 ...
- iOS 发大招 otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法 && 编写通用类的时候关于可变参数的处理
开始 我 以为 这个 alertView 里面 ...的写法 应该 是一个 普通的数组 然 并没有 分享一篇好文 http://www.tekuba.net/program/290/ IOS实现 ...
- [原创]java WEB学习笔记12:一个简单的serlet连接数据库实验
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 【leetcode刷题笔记】Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- poj 1419Graph Coloring 【dfs+补图+计算最大团+计算最大独立集 【模板】】
题目地址:http://poj.org/problem?id=1419 Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total S ...
- Wildfly在Linux下以Service的方式启动 配置步骤
1.前提介绍 在目前项目中使用Wildfly9在linux下部署项目,经常会通过远程的SSH来启动关闭服务.但是通过SHH启动 standalone.sh 的服务,如果关闭窗口服务就会被停掉.所以就想 ...
- js 禁止用户使用Ctrl+鼠标滚轮缩放网页
为什么会有人会使用ctrl+鼠标滚轮缩放网页?坚决禁止! <html> <head> <title>测试</title> <script lang ...
- Python习题-统计日志中访问次数超过限制的IP
#1.1分钟之内ip访问次数超过200次的,就给他的ip加入黑名单#需求分析: #1.读日志,1分钟读一次 #2.获取这1分钟之内所有访问的ip #3.判断ip出现的次数,如果出现200次,那么就加入 ...