ES6 随记(3.3)-- 数组的拓展
上一章请见:
4. ES6 随记(3.2)-- 正则的拓展 & 数值的拓展
4. 拓展
d. 数组的拓展
· Array.from 方法
querySelectorAll 得到并不是一个数组,而是 NodeList;arguments 也不是数组,是个很奇怪的对象。
所以新增了一个 Array.from 方法,将这些可遍历(具有 Iterator 接口)对象轻松转化成真实的数组,
var $page = document.querySelectorAll('.page');
var pageArr = Array.from($page); // [div.page.1, div.page.2]
var temp = pageArr.splice(0, 1); // [div.page.1]
console.log(pageArr); // [div.page.2]
(function(){
var args = Array.from(arguments);
// var args = [...arguments]; // 此方法与上式等效,Array.from 的简约版
args.forEach(function(a){ // arguments 没法直接 forEach
console.log(a); // 1 // 2
});
})(1, 2)
// 可遍历对象有很多,以后讲 Iterator 接口时会再说
console.log(Array.from('abc')); // [1,2,3]
// 类数组对象如果有 length,则按数组的那套申明规则来
console.log(Array.from({1:2, length:2})); // [undefined, 2]
其次,Array.from 可以传递第二个参数,相当于合并了 map 方法。
var arrayLike = '12';
var arr1 = Array.from(arrayLike, x => x * x);
console.log(arr1); // [1, 4];
// 等同于
var arr2 = Array.from(arrayLike).map(x => x * x);
console.log(arr1); // [1, 4];
// 新建一个连续数字的数组
var arr11 = Array.from({ length: 10 }, (n, i) => i);
console.log(arr11); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
· Array.of 方法
其实它就是为了弥补以下这个缺陷而存在
// 是长度还是元素,傻傻分不清楚
console.log(new Array(3)); // [undefined × 3]
console.log(new Array(1, 2)); // [1, 2]
// Array.of 就只会是元素
console.log(Array.of(3)); // [3]
console.log(Array.of(1, 2)); // [1, 2]
· copyWithin(target, start, end) 方法
从 start 开始到 end 结束的元素,覆盖掉从 target 到 target+(end-start) 个长度的元素。
start 传负值为倒数;end 不填为到最后,小于 start 无效,除非是负数,为倒数;这个 start 和 end 的传值规则到哪都是一致的。
console.log([1,2,3,4,5].copyWithin(0, 4)); // [5, 2, 3, 4, 5]
console.log([1,2,3,4,5].copyWithin(0, 3, 2)); // [1, 2, 3, 4, 5]
console.log([1,2,3,4,5].copyWithin(0, 2, -1)); // [3, 4, 3, 4, 5]
console.log([1,2,3,4,5].copyWithin(0, -1)); // [5, 2, 3, 4, 5]
console.log([1,2,3,4,5].copyWithin(0, -2, -1)); // [4, 2, 3, 4, 5]
· find 和 findIndex 方法
参数为一个函数,进行运算后,返回第一个 return true 的元素/索引,否则返回 undefined / -1
console.log([1,4,8,12].find( function(item,index,arr){return item>6;}) ); //8
console.log([1,4,8,12].findIndex( function(item,index,arr){return item>6;}) ); //2
· fill(template, start, end) 方法
传递三个参数,从 start 开始到 end 结束的所有元素用 template 替换掉。
console.log([1,2,3,4].fill(9)); // [9, 9, 9, 9]
console.log([1,2,3,4].fill(9, 1)); // [1, 9, 9, 9]
console.log([1,2,3,4].fill(9, -1)); // [1, 2, 3, 9]
console.log([1,2,3,4].fill(9, 1, -1)); // [1, 9, 9, 4]
· keys() / values() / entries() 数组遍历简化
注:只能用于 for-of 循环
console.log(['x','y'].entries()); // Array Iterator {}
for(var i of ['x', 'y'].keys()) {
console.log(i); // 0 // 1
}
for(var i in ['x', 'y'].keys()) {
console.log(i); // for-in 不运行
}
for(var i of ['x', 'y'].entries()) {
console.log(i); // [0, 'x'] // [1, 'y']
}
for(var [index, item] of ['x', 'y'].entries()) {
console.log(index, item); // 0, 'x' // 1, 'y'
}
for(var i of ['x', 'y'].values()) {
console.log(i); // 'x' // 'y' // 2017.04.19 暂不支持
}
· includes(value, position) 方法
判断 position 位置是否为 value;或 position 为空,判断该数组是否存在某元素为 value。
它相比 indexOf 更直观有语义,也支持对 NaN 的判断
var arr = [1,2,3,4,NaN];
console.log(arr.includes(2)); // true
console.log(arr.includes(5)); // false
console.log(arr.includes(2, 1));// true
console.log(arr.includes(2, 2));// false
console.log(arr.includes(NaN)); // true
console.log(arr.indexOf(NaN)); // -1
· 数组空位
ES5 对空位的设置是非常不统一的,
首先我们得知道 [,] 和 [undefined, undefined] 是不一样的,
let arr1 = [,0], arr2 = [undefined, undefined];
// n in 代表 n 号位有值,它和对象的 in 不一样
console.log(1 in arr1); // true
// 可见,数组空值并不是 undefined
console.log(0 in arr1, 0 in arr2); // false, true
其次,ES5 的方法也大多直接忽略掉了空值
// forEach filter every some map 会跳过空值
[,'a',undefined].forEach((x,i) => console.log(i)); // 1
console.log(['a',,'b'].filter(x => true)); // ['a','b']
console.log([,'a'].every(x => x==='a')); // true
console.log([,'a'].some(x => x !== 'a')); // false // map 稍微有点特殊,虽然过程跳过了,但结果还是保留了这个空值
console.log([,undefined,'a'].map(x => 1)); // [,1,1] // join 和 toString 倒是把空值当成 undefined
console.log([,'a',undefined,null].join('#')); // "#a##"
console.log([,'a',undefined,null].toString()); // ",a,,"
虽然 ES6 没有将这个 bug 进行调整,但在新方法中加入了对空值的判断。
即 copyWithin / fill / for-of / keys 等都将不忽略空值,以 undefined 来对待。
而想要完善 ES5 对空值的判断,那么就用 Array.from 转化一下吧,它将把空值变成 undefined。
console.log([,,]); // [undefined × 2] // 待 x 数字其实都是空值
console.log(Array.from([,,])); // [undefined, undefined]
本文部分转载自 阮一峰 的 ECMAScript 6 入门
ES6 随记(3.3)-- 数组的拓展的更多相关文章
- ES6 随记(3.4.1)-- 函数的拓展(参数默认值,扩展运算符)
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...
- ES6 数组方法拓展
ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...
- ES6 随记(3.2)-- 正则的拓展 & 数值的拓展
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. 拓展 b. 正则的拓展 首先又是关于 ...
- ES6 随记(3.1)-- 字符串的拓展
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 4. 拓展 a. 字符串的拓展 有些字符需要 4 个字节储存,比如 \uD83D\uDE80 ...
- ES6 随记(2)-- 解构赋值
上一章请见: 1. ES6 随记(1)-- let 与 const 3. 解构赋值 a. 数组的解构赋值 let [a1, b1, c1] = [1, 2, 3]; console.log(a1, b ...
- 你好,C++(23) 4.4.2 工资程序成长记:用数组处理批量数据,用循环结构执行重复动作
4.4 从语句到程序 了解了各种表达式和语句之后,就相当于掌握了写作文要用到的词语和句子,但是,仅有词语和句子是无法构成一篇有意义的文章的.要完成一篇文章,先需要确定这篇文章的结构,是先分述再总述, ...
- es6基础系列五--数组的拓展
Array.from() 用于将两类对象转为真正的数组,类似数组对象和可遍历对象(包括数据结构Set和Map)转化为数组 格式:Array.from(arrayLike[, mapFn[, thisA ...
- ES6 数组的拓展(五)
一.扩展运算符(...)将数组转化为以,分割的字符串eg: console.log(...[1,2,3,4]); //1 2 3 4 将字符串转化为数组eg: console.log([...'hel ...
- ES6数组的拓展
扩展运算符 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) // 1 2 3 c ...
随机推荐
- String painter (hdu 2476 DP好题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意: 给出两个等长的串S, T, 要将S变成T, 每次可以把S的连续的一段变成相同的字母 ...
- opencv2.4.9+VS2010配置
opencv2.4.9 https://pan.baidu.com/s/15b5bEY65R4CptayEYVAG4A 安装包路径:D:\文件及下载相关\文档\Tencent Files\845235 ...
- 2、手把手教React Native实战之从React到RN
###React简介 RN是基于React设计,了解React有助于我们开发RN应用: React希望将功能分解化,让开发变得像搭积木一样,快速而且可维护 React主要有如下3个特点: *作为UI( ...
- JDBC批量操作性能提升
JDBC 当使用INSERT INTO....VALUES()语句批量插入的时候,应该使用JDBC的PreparedStatement的批量操作方法,而不是採用一条一条运行的方法. 比如(来源:htt ...
- android应用安全——组件通信安全(Intent)
这里主要涉及到了Activity.Content Provider.Service.Broadcast Receiver等.这些如果在Androidmanifest.xml配置不当,会被其他应用调用, ...
- hdu2469(计算几何)
枚举所有可能的半径,然后将所有满足这个半径的点按角度(与x轴正半轴的夹角)排序. 然后一遍扫描求出在这个半径下选k个点所需的最小面积 . 思路还是比较简单,实现略有些繁琐. 要先将点的坐标转换为角度. ...
- ORA-12505, TNS:listener does not currently know of SID given in connect desc
数据库名(数据库服务名)配置错误.
- [Algorithms] Counting Sort
Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...
- 记Spring-SpringMVC-Mybatis框架搭建
1.spring相关架包的下载 云盘下载地址:https://pan.baidu.com/s/1o8sk8Ee 官网下载地址:http://repo.springsource.org/libs-rel ...
- Kubernetes初探:原理及实践应用
总体概览 如下图所示是我初步阅读文档和源代码之后整理的总体概览,基本上可以从如下三个维度来认识Kubernetes. 操作对象 Kubernetes以RESTFul形式开放接口,用户可操作的REST对 ...