上一章请见:

1. ES6 随记(1)-- let 与 const

2. ES6 随记(2)-- 解构赋值

3. ES6 随记(3.1)-- 字符串的拓展

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)-- 数组的拓展的更多相关文章

  1. ES6 随记(3.4.1)-- 函数的拓展(参数默认值,扩展运算符)

    上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...

  2. ES6 数组方法拓展

    ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...

  3. ES6 随记(3.2)-- 正则的拓展 & 数值的拓展

    上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. 拓展 b. 正则的拓展 首先又是关于 ...

  4. ES6 随记(3.1)-- 字符串的拓展

    上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 4. 拓展 a. 字符串的拓展 有些字符需要 4 个字节储存,比如 \uD83D\uDE80 ...

  5. ES6 随记(2)-- 解构赋值

    上一章请见: 1. ES6 随记(1)-- let 与 const 3. 解构赋值 a. 数组的解构赋值 let [a1, b1, c1] = [1, 2, 3]; console.log(a1, b ...

  6. 你好,C++(23) 4.4.2 工资程序成长记:用数组处理批量数据,用循环结构执行重复动作

    4.4  从语句到程序 了解了各种表达式和语句之后,就相当于掌握了写作文要用到的词语和句子,但是,仅有词语和句子是无法构成一篇有意义的文章的.要完成一篇文章,先需要确定这篇文章的结构,是先分述再总述, ...

  7. es6基础系列五--数组的拓展

    Array.from() 用于将两类对象转为真正的数组,类似数组对象和可遍历对象(包括数据结构Set和Map)转化为数组 格式:Array.from(arrayLike[, mapFn[, thisA ...

  8. ES6 数组的拓展(五)

    一.扩展运算符(...)将数组转化为以,分割的字符串eg: console.log(...[1,2,3,4]); //1 2 3 4 将字符串转化为数组eg: console.log([...'hel ...

  9. ES6数组的拓展

    扩展运算符 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) // 1 2 3 c ...

随机推荐

  1. 如何连接OracleRAC

    查看tnsname 查看服务器上tnsname.ora内容: 位置:/oracle/db/product/11.2.0/network/admin/tnsname.ora   连接rac 根据以上信息 ...

  2. Lumen rule

    之前写了了laravel表单验证的生命周期:https://www.cnblogs.com/cxscode/p/7561277.html 今天来总结一下lumen的Validator的一些使用心得 可 ...

  3. Linux 系统时间设置

    from:https://blog.csdn.net/yjh314/article/details/51669238 今早看到一台机器时间对不上,本以为系统时间与网络北京时间不同步,就在终端命令执行网 ...

  4. 百度订单Api注意事项

    背景介绍: 申请的百度地图API,采用javascript sdk方式 页面引用 问题1:更换域名导致定位插件不能用 需要修改百度地图-应用中的白名单设置,按照规则添加新的域名 问题2:http与ht ...

  5. Objective-C规范注释心得——同时兼容appledoc(docset、html)与doxygen(html、pdf)的文档生成

    作者:zyl910 手工写文档是一件苦差事,幸好现在有从源码中抽取注释生成文档的专用工具.对于Objective-C来说,目前最好用的工具是appledoc和doxygen.可是这两种工具对于注释的要 ...

  6. zTree 树形控件 ajax动态加载数据

    很久没搞过树形控件了 , 再次接触看官网文档有点没懂,于是在网上找了个代码copy上,但数据是写死的,就想这在用ajax异步取出数据替换,下面是js代码 <SCRIPT type="t ...

  7. delphi------项目类型

    Console Application:控制台应用程序 writeln('HelloWorld'); //接收用户输入字符 readln: //直到用户输入回车结束 VCL Forms Applica ...

  8. dist\_wepylogs.js

    console.log('WePY开启错误监控'); console.warn("CLI报错:WARNING IN : src\pages\cloundAd.vue\n[xmldom war ...

  9. SQL中的函数 •Aggregate 函数 •Scalar 函数

    合计函数  :Aggregate是针对一系列值的操作,返回一个单一的值 Scalar 函数是针对一个单一的值的操作,返回基于输入值的一个单一值 合计函数: AVG()返回某列的平均值:COUNT()返 ...

  10. AHOI2019退役记

    $DAY\quad -1$: 连作业都不写了来刷题... 希望能长点$RP$吧... 反正也是抱着退役的心情来考试... 我要是到了周日还不出长门我就退游!!! $DAY\quad 0$: 早上一起来 ...