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 ...
随机推荐
- 【BZOJ】3392: [Usaco2005 Feb]Part Acquisition 交易(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=3392 同1674 #include <cstdio> #include <cstr ...
- 我的第一个reactnative
由于在做极光推送,前端使用的框架是reactnative,后台写好后为了测试一下,所以按照react官方的教程搭了遍react. 开发环境: 1.windows 7(建议各位如果开发react的最好还 ...
- SQL Server Profiler的简单使用(监控mssql)
SQL Server Profiler可以检测在数据上执行的语句,特别是有的项目不直接使用sql语句,直接使用ORM框架的系统处理数据库的项目,在调试sql语句时,给了很大的帮助. 之前写了使用SQL ...
- IOS开发之 -- 线程初识
对于开发者来说,多线程永远有这一层神秘的色彩,似乎是一到迈步过去的坎,在同步.异步.串行.并行.死锁这几个名字当中,逐渐的放弃治疗,下面就多线程,谈一下自己的认识,理解的肯定不全面,只是一些简单的皮毛 ...
- 认识tornado(一)
tornado 源码包中 demos 目录下包含一些示例程序,就从最简单的 helloworld.py 来看一个 tornado 应用程序的代码结构. 完整的实例程序如下: 01 #!/usr/bin ...
- leveldb学习笔记
LevelDB由 Jeff Dean和Sanjay Ghemawat开发. LevelDb是能够处理十亿级别规模Key-Value型数据持久性存储的C++ 程序库. 特别如下: 1.LevelDb是一 ...
- iOS 去掉navgationbar 底部线条
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent]; [[UINavigationBar appearance] ...
- linux时间格式化
echo `date +'[%Y-%m-%d %H:%M:%S]'`
- 巨蟒python全栈开发-第12天 生成器函数 各种推导式 yield from
一.今日主要内容总览(重点) 1.生成器(目的:帮助我们创建对象) (1)生成器的本质就是迭代器 (2)一个一个的创建对象 (3)创建生成器的方式: 1.生成器函数 2.通过生成器表达式来获取生成器 ...
- Struts 上传文件
1. 客户端注意事项 method="post" enctype="multipart/form-data" <input type="file ...