• 【JavaScript】Array 实例方法(一)


    以下内容为学习记录,可以参考 MDN 原文。

    环境

    • node v12.18.1
    • npm 6.14.5
    • vscode 1.46
    • Microsoft Edge 83

    concat

    concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    const array3 = array1.concat(array2);
    
    console.log(array3);
    // expected output: Array ["a", "b", "c", "d", "e", "f"]
    

    copyWithin

    copyWithin() 方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度。

    const array1 = ['a', 'b', 'c', 'd', 'e'];
    
    // copy to index 0 the element at index 3
    console.log(array1.copyWithin(0, 3, 4));
    // expected output: Array ["d", "b", "c", "d", "e"]
    
    // copy to index 1 all elements from index 3 to the end
    console.log(array1.copyWithin(1, 3));
    // expected output: Array ["d", "d", "e", "d", "e"]
    

    entries

    entries() 方法返回一个新的 Array Iterator 对象,该对象包含数组中每个索引的键/值对。

    const array1 = ['a', 'b', 'c'];
    
    const iterator1 = array1.entries();
    
    console.log(iterator1.next().value);
    // expected output: Array [0, "a"]
    
    console.log(iterator1.next().value);
    // expected output: Array [1, "b"]
    

    every

    every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

    const isBelowThreshold = (currentValue) => currentValue < 40;
    
    const array1 = [1, 30, 39, 29, 10, 13];
    
    console.log(array1.every(isBelowThreshold));
    // expected output: true
    

    fill

    fill() 方法将数组中的所有元素更改为静态值,从开始索引(默认为 0)到结束索引(默认为 array.length)。它返回修改后的数组。

    const array1 = [1, 2, 3, 4];
    
    // fill with 0 from position 2 until position 4
    console.log(array1.fill(0, 2, 4));
    // expected output: [1, 2, 0, 0]
    
    // fill with 5 from position 1
    console.log(array1.fill(5, 1));
    // expected output: [1, 5, 5, 5]
    
    console.log(array1.fill(6));
    // expected output: [6, 6, 6, 6]
    

    filter

    filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

    const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    
    const result = words.filter(word => word.length > 6);
    
    console.log(result);
    // expected output: Array ["exuberant", "destruction", "present"]
    

    find

    find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。

    const array1 = [5, 12, 8, 130, 44];
    
    const found = array1.find(element => element > 10);
    
    console.log(found);
    // expected output: 12
    

    findIndex

    findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回 -1。

    const array1 = [5, 12, 8, 130, 44];
    
    const isLargeNumber = (element) => element > 13;
    
    console.log(array1.findIndex(isLargeNumber));
    // expected output: 3
    

    forEach

    forEach() 方法为每个数组元素执行一次提供的函数。

    const array1 = ['a', 'b', 'c'];
    
    array1.forEach(element => console.log(element));
    
    // expected output: "a"
    // expected output: "b"
    // expected output: "c"
    
  • 相关阅读:
    【Ansible】 各种模块
    【Python】 linecache模块读取文件
    【Ansible】 基于SSH的远程管理工具
    【Python】 更棒的Excel操作模块xlwings
    【Python】 docker-py 用Python调用Docker接口
    【Docker】 Swarm简单介绍
    【Docker】 积累
    【Bootstrap】 一些提示信息插件
    配置 -- php运行报Call to undefined function curl_init()的解决办法
    windows下svn钩子实现每次提交更新至web目录
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/13604891.html
Copyright © 2020-2023  润新知