美文网首页
Array扩展

Array扩展

作者: 牛耀 | 来源:发表于2018-09-25 22:48 被阅读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的值
 /*
   需求:
   1. 输出第一个6的下标
   2. 输出最后一个6的下标
   3. 输出所有元素的值和下标
   4. 根据arr产生一个新数组,要求每个元素都比原来大10
   5. 根据arr产生一个新数组, 返回的每个元素要大于4
   */
  var arr = [2,4,3,1,2,6,5,4];
  console.log(arr.indexOf(4));
  console.log(arr.lastIndexOf(4));
  arr.forEach(function(item, index){
    console.log(item, index);
  })
  var arr1 = arr.map(function(item, index) {
    return item + 10;
  });
  console.log(arr1);
  var arr2 = arr.filter(function(item, index){
    return item > 3;
  });
  console.log(arr, arr2);

相关文章

网友评论

      本文标题:Array扩展

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