美文网首页
85-数组高级API-数组查找

85-数组高级API-数组查找

作者: 仰望_IT | 来源:发表于2019-05-02 17:03 被阅读0次
  • 数组查找方式

    • 1. 传统查找方式
          //         0  1  2  3  4
          let arr = [3, 2, 6, 7, 6];
      
          // 从左至右查找, 找到返回索引, 找不到返回-1
          let index1 = arr.indexOf(6);
          console.log(index1);    // 2
          // 从右至左查找, 找到返回索引, 找不到返回-1
          let index2 = arr.lastIndexOf(6);
          console.log(index2);    // 4
          // 从左至右查找, 找到返回true, 找不到返回false
          let result = arr.includes(6);
          console.log(result);    // true
      
    • 2.数组的 findIndex 方法
      • findIndex方法定制版的indexOf, 找到返回索引, 找不到返回 -1
          //         0  1  2  3  4
          let arr = [3, 2, 6, 7, 6];
      
          let index = arr.findIndex(function (currentValue, currentIndex, currentArray) {
              if (currentValue === 6){
                  return true;
              }
          });
          console.log(index);  // 2
      
    • 3.数组的 find 方法
      • find方法如果找到了就返回找到的元素, 如果找不到就返回 undefined

          //         0  1  2  3  4
          let arr = [3, 2, 6, 7, 6];
        
          let value = arr.find(function (currentValue, currentIndex, currentArray) {
              if (currentValue === 6) {
                  return true;
              }
          });
          console.log(value);  // 6
        

findIndex方法返回索引, find方法返回找到的元素

相关文章

  • 85-数组高级API-数组查找

    数组查找方式1. 传统查找方式 // 0 1 2 3 4 let arr = ...

  • 84-数组高级API-数组遍历

    遍历数组1. 利用传统循环来遍历数组 let arr = [1, 3, 5, 7, 9]; for (...

  • 36个常用js代码片段

    数组 Array 数组去重 查找数组最大 查找数组最小 返回已 size 为长度的数组分割的原数组 检查数组中某元...

  • 工作中常用的JavaScript函数片段

    数组 Array 1、数组去重 2、查找数组最大 3、查找数组最小 4、返回已size为长度的数组分割的原数组 5...

  • INDEX MATCH

    = INDEX(数组,行号)--一列的位置= MATCH( 查找值,查找数组,匹配类型)=INDEX(数组, ...

  • 顺序查找

    1、顺序查找a为数组,n为查找的数组个数,key为要查找的关键字; 2、顺序查找_哨兵 3、折半查找算法假设数组a...

  • JavaScript中数组的常用操作

    数组的遍历 数组的映射 数组的简化 数组的连接 获取数组的片段 数组的拷贝 查找数组 数组去重

  • 数据结构和算法面试题整理

    #数组 - [查找数组中第二小的元素] - [查找第一个没有重复的数组元素] - [合并 2 个排序好的数组] -...

  • 86-数组高级API-数组的过滤和映射

    数组的过滤数组的 filter 方法作用: 将满足条件的元素添加到一个新的数组中 // 0 1...

  • 去除数组中重复元素

    1.借助控对象;2.使用临时数组+ 查找数组下标 ;3.map() 遍历数组 查找下标 临时数组;4.filter...

网友评论

      本文标题:85-数组高级API-数组查找

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