zoukankan      html  css  js  c++  java
  • [Javascript] Array methods in depth

    indexOf is used to search for a value or reference inside of an array. In this lesson we first look at what values are returned when a search is successful vs when it's unsuccessful. Then we move onto a technique that shows how to use the return value to create a boolean flag that can be checked easily. We end by filtering 1 array based on the existence of a value in a whitelist array.

    var people = ['Wan', 'John', 'Kate', 'Joe'];
    var indexJoe = people.indexOf('Joe');
    var findJoe = people.indexOf('Joe') > -1;
    
    console.log(indexJoe, findJoe); //3, true

    indexOf can take second params for telling the startLookingIndex:

    var people = ['Wan','Joe', 'John', 'Kate'];
    var indexJoe = people.indexOf('Joe');
    var findJoe = people.indexOf('Joe', 2) > -1;
    
    console.log(indexJoe, findJoe); //1, false
    
    var findJoe = people.indexOf('Joe', 1) > -1;
    console.log(indexJoe, findJoe); //1, true

    Example:

    var whitelist = ['.css', '.js'];
    
    var events = [
      {
        file: 'css/core.css'
      },
      {
        file: 'js/app.js'
      },
      {
        file: 'index.html'
      }
    ];
    
    var filtered = events.filter(event => {
      var ext = event.file.substr(event.file.lastIndexOf('.'));
      return whitelist.indexOf(ext) > -1;
    });
    
    console.log(filtered);
  • 相关阅读:
    with ,Row_Number,DateDiff,DateAdd用法学习
    jmeter 读取mysql数据库
    fidder 自动保存请求内容
    redis 常用方法整理
    解决:EXCEL复制粘贴,精度丢失
    MYSQL 创建常见问题
    MYSQL 存储过程、函数、临时表、游标
    MYSQL 测试常用语句使用技巧
    3-6
    selenium3 下载、配置
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5090231.html
Copyright © 2011-2022 走看看