• es6 includes的用法----判断一个字符串或数组是否包含一个指定的值


    句法:

    str.includes(searchString , [position]) 

    searchString在此字符串内搜索的字符串。

    position 可选的字符串中开始搜索的位置searchString(默认为0)。

    返回值:

    true:如果搜索字符串在给定字符串内的任何地方找到;返回true 

    false:如果没有找到返回false

    描述:

    此方法可让您确定一个字符串是否包含另一个字符串。

    includes()方法区分大小写。例如,以下表达式返回false:

    'Blue Whale'.includes('blue'); // returns false

    运用:

    var str = 'To be, or not to be, that is the question.';
    
    console.log(str.includes('To be'));       // true
    console.log(str.includes('question'));    // true
    console.log(str.includes('nonexistent')); // false
    console.log(str.includes('To be', 1));    // false
    console.log(str.includes('TO BE'));       // false

    填充工具:

    此方法已添加到ECMAScript 2015规范中,可能尚未在所有JavaScript实现中提供。但是,您可以轻松地填充此方法:

    if (!String.prototype.includes) {
      String.prototype.includes = function(search, start) {
        'use strict';
        if (typeof start !== 'number') {
          start = 0;
        }
        
        if (start + search.length > this.length) {
          return false;
        } else {
          return this.indexOf(search, start) !== -1;
        }
      };
    }

    不过填充工具是什么。最后一点没有看懂

    补充es5

    es5中是用indexOf的命令来查找的,存在的返回的是索引值,不存在返回-1,但是NaN查找不出来,因为NaN!==NaN

  • 相关阅读:
    php数组函数-array_push()
    php数组函数-array_pop()
    php数组函数-array_pad()
    php数组函数-array_merge()
    php数组函数-array_map()
    php数组函数-array_keys()
    php数组函数-array_key_exists()
    php数组函数-array_intersect()
    php数组函数-array_flip()
    php数组函数-array_filter()
  • 原文地址:https://www.cnblogs.com/haonanya/p/9004419.html
Copyright © 2020-2023  润新知