美文网首页
数组的扩展

数组的扩展

作者: 向上而活 | 来源:发表于2020-06-16 16:12 被阅读0次
  1. Array.prototype.indexOf(value) : 得到值在数组中的第一个下标
  2. Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
  3. Array.prototype.forEach(function(item, index){}) : 遍历数组
  4. Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值
  5. Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值
<script type="text/javascript">
  /*
   需求:
   1. 输出第一个6的下标
   2. 输出最后一个6的下标
   3. 输出所有元素的值和下标
   4. 根据arr产生一个新数组,要求每个元素都比原来大10
   5. 根据arr产生一个新数组, 返回的每个元素要大于4
   */

    var arr = [1, 4, 6, 2, 5, 6];
    console.log(arr.indexOf(6));//2
    //Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
    console.log(arr.lastIndexOf(6));//5

    //Array.prototype.forEach(function(item, index){}) : 遍历数组
    arr.forEach(function (item, index) {
        console.log(item, index);
    });

    //Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值
    var arr1 = arr.map(function (item, index) {
        return item + 10
    });
    console.log(arr, arr1);

    //Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值
    var arr2 = arr.filter(function (item, index) {
        return item > 4
    });
    console.log(arr, arr2);


</script>

相关文章

  • 3.数组扩展

    1)cat函数扩展结果=cat(1或2,数组1,数组2) 3)水平连接扩展结果=horzcat(数组1,数组2,。...

  • ES6扩展

    字符串的扩展正则的扩展数值的扩展数组的扩展函数的扩展对象的扩展

  • ES6数组扩展

    二、数组扩展

  • 数组的扩展

    扩展运算符 含义 扩展运算符(spread)是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗...

  • 数组的扩展

    Aaary类是一个函数,返回数组 Array.of() Array.form(数组/类数组),返回一个数组 在原型...

  • 数组的扩展

    数组的扩展 扩展运算符...Array类似 ...restrest参数的逆运算,所有实现iterator接口的类数...

  • 数组的扩展

    1、扩展运算符 扩展运算符为三个点(...),可以将数组中的值以逗号形式序列化。 2、数组的find()和find...

  • 数组的扩展

    ** 参考**书籍:ECMAScript 6 入门作者:阮一峰 Array.of()Array.of方法用于将一组...

  • 数组的扩展

    扩展运算符(...) 基本用法 该运算符作用正好与rest参数作用相反,用于展开数组为参数序列,用逗号分割。 应用...

  • 数组的扩展

    如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错。 扩展运算符还可以将字符串转为真正的数组。 上面...

网友评论

      本文标题:数组的扩展

      本文链接:https://www.haomeiwen.com/subject/amdrxktx.html