es6笔记2^_^array

一、Array.from()

  Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Set和Map)。
if(1){
    let list = document.querySelectorAll('ul.fancy li');
    Array.from(list).forEach(function (li) {
       console.log(li);
    });
}
  上面代码中,querySelectorAll方法返回的是一个类似数组的对象,只有将这个对象转为真正的数组,才能使用forEach方法。
  任何有length属性的对象,都可以通过Array.from方法转为数组。
    let array1 = Array.from({ 0: "a", 1: "b", 2: "c", length: 3 });
    console.log((array1));    // [ "a", "b" , "c" ]
  Array.from()还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理
    let array = [0,1,2,3,4];
    let arrNew = Array.from(array, x => x * x);
    console.log(arrNew);
// 等同于
//let arrNew = Array.from(array).map(x => x * x);
  下面的例子将数组中布尔值为false的成员转为0。
    let arr1=Array.from([1, , 2, , 3], (n) => n || 0);
    console.log(arr1);// [1, 0, 2, 0, 3]
  Array.from()的一个应用:将字符串转为数组,然后返回字符串的长度。这样可以避免JavaScript将大于uFFFF的Unicode字符,算作两个字符的bug。
    function countSymbols(string) {
        return Array.from(string).length;
    }
    console.log(countSymbols('nick'));
    let arg;
    function test(){
        arg=arguments;
        console.log(arg);//类数组
        console.log(arg instanceof Array);
    }
    test(1,2);
    console.log(Array.from(arg,x=>x+1));
    let person={0:'张三',1:'李四',2:'王五',length:3};
    console.log(Array.from(person));

二、Array.of()将值转换为数组

    //Array.of方法用于将一组值,转换为数组。
    console.log(Array.of(3, 11, 8));// [3,11,8]
    console.log(Array.of(3));// [3]
    console.log(Array.of(3,1).length);// 1

三、arr.find(callback[, thisArg])找出第一个符合条件的数组成员和位置

  数组实例的find方法,用于找出第一个符合条件的数组成员。   

  它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员。如果没有符合条件的成员,则返回undefined。

    let array = [1, 4, -5, 10].find((n) => n < 0);
    console.log("array:", array);
//上面代码找出数组中第一个小于0的成员。
    let array1 = [1, 5, 10, 15].find(function(value, index, arr) {
        console.log(value);
        console.log(index);
        console.log(arr);
        return value > 9;
    });
    console.log(array1);  // 10
  上面代码中,find方法的回调函数可以接受三个参数,依次为当前的值、当前的位置和原数组。
  数组实例的findIndex方法,用法与find方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1。
    let index = [1, 5, 10, 15].findIndex(function(value, index, arr) {
        return value > 9;
    })
    console.log(index);  // 2
  这两个方法都可以接受第二个参数,用来绑定回调函数的this对象。
  另外,这两个方法都可以发现NaN,弥补了数组的IndexOf方法的不足。
    console.log([NaN].indexOf(NaN));// -1
    console.log([NaN].findIndex(y => Object.is(NaN, y)));// 0
    console.log([NaN].findIndex(function(v){if(Object.is(NaN, v)){return true;}}));// 0

  上面代码中,indexOf方法无法识别数组的NaN成员,但是findIndex方法可以借助Object.is方法做到。

四、fill()使用给定值,填充一个数组。

      let arr = ['a', 'b', 'c'].fill(7)
    console.log(arr);  // [7, 7, 7]
    let newArr = new Array(3).fill(7)
    console.log(newArr);  // [7, 7, 7]
    /*上面代码表明,fill方法用于空数组的初始化非常方便。数组中已有的元素,会被全部抹去。
     fill()还可以接受第二个和第三个参数,用于指定填充的起始位置和结束位置。*/
    let newArr1 = ['a', 'b', 'c'].fill(7, 1, 2)
    console.log(newArr1);   // ['a', 7, 'c']

五、循环entries()、keys()、values()

ES6提供三个新的方法:
entries() keys() values()
用于遍历数组。它们都返回一个遍历器,可以用for...of循环进行遍历,
唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历。
    for (let index of ['a', 'b'].keys()) {
        console.log(index);// 0 1
    }

    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"
    }

此篇全部代码:

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>es6-array</title>
    <!-- 加载Traceur编译器 -->
   <script src="http://google.github.io/traceur-compiler/bin/traceur.js" type="text/javascript"></script>
    <script src="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script>
    <!-- 将Traceur编译器用于网页 -->
    <script src="http://google.github.io/traceur-compiler/src/bootstrap.js" type="text/javascript"></script>
    <script>
/* 1.Array.from()将两类对象转为真正的数组
Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Set和Map)。
一个转换类数组对象到数组的一个示例:*/
/*
let list = document.querySelectorAll('ul.fancy li');
Array.from(list).forEach(function (li) {
    console.log(li);
});*/
/*上面代码中,querySelectorAll方法返回的是一个类似数组的对象,只有将这个对象转为真正的数组,才能使用forEach方法。
任何有length属性的对象,都可以通过Array.from方法转为数组。*/
if(1){
    let array1 = Array.from({ 0: "a", 1: "b", 2: "c", length: 3 });
    console.log((array1));    // [ "a", "b" , "c" ]
//Array.from()还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理。
    let array = [0,1,2,3,4];
    let arrNew = Array.from(array, x => x * x);
    console.log(arrNew);
// 等同于
//let arrNew = Array.from(array).map(x => x * x);
//下面的例子将数组中布尔值为false的成员转为0。
    let arr1=Array.from([1, , 2, , 3], (n) => n || 0);
    console.log(arr1);// [1, 0, 2, 0, 3]
//Array.from()的一个应用:将字符串转为数组,然后返回字符串的长度。这样可以避免JavaScript将大于uFFFF的Unicode字符,算作两个字符的bug。

    function countSymbols(string) {
        return Array.from(string).length;
    }
    console.log(countSymbols('nick'));

    let arg;
    function test(){
        arg=arguments;
        console.log(arg);//类数组
        console.log(arg instanceof Array);
    }
    test(1,2);
    console.log(Array.from(arg,x=>x+1));
    let person={0:'张三',1:'李四',2:'王五',length:3};
    console.log(Array.from(person));
}
    /*2 .Array.of()将值转换为数组*/
    //Array.of方法用于将一组值,转换为数组。
    console.log(Array.of(3, 11, 8));// [3,11,8]
    console.log(Array.of(3));// [3]
    console.log(Array.of(3,1).length);// 1
/*
3.arr.find(callback[, thisArg])找出第一个符合条件的数组成员和位置
数组实例的find方法,用于找出第一个符合条件的数组成员。
它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员。
如果没有符合条件的成员,则返回undefined。
*/
if(1){
    let array = [1, 4, -5, 10].find((n) => n < 0);
    console.log("array:", array);
//上面代码找出数组中第一个小于0的成员。
    let array1 = [1, 5, 10, 15].find(function(value, index, arr) {
        console.log(value);
        console.log(index);
        console.log(arr);
        return value > 9;
    });
    console.log(array1);  // 10
    /*
     上面代码中,find方法的回调函数可以接受三个参数,依次为当前的值、当前的位置和原数组。
     数组实例的findIndex方法,用法与find方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1。
     */

    let index = [1, 5, 10, 15].findIndex(function(value, index, arr) {
        return value > 9;
    })
    console.log(index);  // 2
    /*
     这两个方法都可以接受第二个参数,用来绑定回调函数的this对象。
     另外,这两个方法都可以发现NaN,弥补了数组的IndexOf方法的不足。
     */
    console.log([NaN].indexOf(NaN));// -1
    console.log([NaN].findIndex(y => Object.is(NaN, y)));// 0
    console.log([NaN].findIndex(function(v){if(Object.is(NaN, v)){return true;}}));// 0
//上面代码中,indexOf方法无法识别数组的NaN成员,但是findIndex方法可以借助Object.is方法做到。
}
/*4.fill()填充数组
fill()使用给定值,填充一个数组。*/
if(1){
    let arr = ['a', 'b', 'c'].fill(7)
    console.log(arr);  // [7, 7, 7]

    let newArr = new Array(3).fill(7)
    console.log(newArr);  // [7, 7, 7]
    /*上面代码表明,fill方法用于空数组的初始化非常方便。数组中已有的元素,会被全部抹去。
     fill()还可以接受第二个和第三个参数,用于指定填充的起始位置和结束位置。*/

    let newArr1 = ['a', 'b', 'c'].fill(7, 1, 2)
    console.log(newArr1);   // ['a', 7, 'c']
}
/*
    5.三个新的方法
ES6提供三个新的方法:
entries() keys() values()
用于遍历数组。它们都返回一个遍历器,可以用for...of循环进行遍历,
唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历。
*/
    for (let index of ['a', 'b'].keys()) {
        console.log(index);// 0 1
    }

    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"
    }

</script>
</head>
<body>
<ul class="fancy">
    <li>1</li>
    <li>2</li>
</ul>
</body>
</html>
View Code

此篇终,待续……



原文地址:https://www.cnblogs.com/puyongsong/p/6274998.html