填坑-十万个为什么?(6)

简介:很多概念不清或忘记,重新构建自己的知识体系。每天问自己1~多个问题。我是菜鸟 成为大神之路!

1. 判断字符串中是否存在某个字符或字符串?
var str = 'Hello world!';
复制代码

String对象的方法➡ECMAScript 5

①String.prototype.indexOf(String) 方法(推荐使用⭐⭐⭐⭐)

可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。

console.log(str.indexOf("w") !== -1 );  // true
console.log(str.indexOf("wr") !== -1 );  // false
复制代码
②String.prototype.search(regexp) 方法 ?

用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。如果没有找到任何匹配的子串,则返回 -1。

console.log(str.search("w") !== -1 );  // true
console.log(str.search("wr") !== -1 );  // false
复制代码
③String.prototype.match(regexp) 方法

参数:可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
返回值:如果字符串匹配到了表达式,会返回一个数组,数组的第一项是进行匹配完整的字符串,之后的项是用圆括号捕获的结果。如果没有匹配到,返回null

var reg = RegExp(/w/);
var reg1 = /wr/
console.log(str.match(reg));//["w", index: 6, input: "Hello world!", groups: undefined]
console.log(str.match(reg1));//null
复制代码

执行结果

String对象的方法➡ECMAScript 6 (这三个方法都支持第二个参数,表示开始搜索的位置)

④String.prototype.includes(String) 方法(推荐使用⭐⭐⭐⭐)

返回布尔值,表示是否找到了参数字符串。

str.startsWith('Hello') // true
str.startsWith('world', 6) // true
复制代码
⑤String.prototype.startsWith(String) 方法

返回布尔值,表示参数字符串是否在原字符串的头部。

str.endsWith('!') // true
str.endsWith('Hello', 5) // true
复制代码
⑥String.prototype.endsWith(String) 方法

返回布尔值,表示参数字符串是否在原字符串的尾部。

str.includes('o') // true
str.includes('Hello', 6) // false
复制代码

RegExp 对象方法

⑦RegExp.prototype.test(String) 方法

用于检索字符串中指定的值。返回 true 或 false。

var reg = RegExp(/w/);
var reg1 = /wr/;
console.log(reg.test(str)); // true
console.log(reg1.test(str)); // false
复制代码
⑧RegExp.prototype.exec(String) 方法

用于检索字符串中的正则表达式的匹配。返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。

var reg = RegExp(/w/);
var reg1 = /wr/;
console.log(reg.exec(str)); // ["w", index: 6, input: "Hello world!", groups: undefined]
console.log(reg1.exec(str)); // null
复制代码

2. 判断Array数组中是否存在某个元素?
//创建数组
var arr = ["one","two","three"];
//var arr=new Array(10);//这里就创建了一个初始化大小为10的数组
//arr.push("one");
//注意:当使用数组大小操作初始化大小时,数组会自动被撑大,不会像C语言那样发生错误.动态增长是js数组的一个性质.
复制代码
①Array.prototype.includes(String,[可选]fromIndex) Es6(推荐使用⭐⭐⭐⭐) ?

方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。
如果fromIndex 大于等于数组长度 ,则返回 false 。该数组不会被搜索。

console.log(arr.includes('one')); // true
console.log(arr.includes('onef')); // false

console.log(arr.includes('one', 4)); // false 如果`fromIndex` 大于等于数组长度 ,则返回 false 。该数组不会被搜索。
console.log(arr.includes('onef', 4)); // false 如果`fromIndex` 大于等于数组长度 ,则返回 false 。该数组不会被搜索。

console.log(arr.includes('one', -100)); // true
console.log(arr.includes('onef', -100)); // false
复制代码
②Array.prototype.indexOf(String,fromIndex)方法(推荐使用⭐⭐⭐⭐)

返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。
fromIndex开始查找的位置。如果该索引值大于或等于数组长度,意味着不会在数组里查找,返回-1。如果参数中提供的索引值是一个负值,则将其作为数组末尾的一个抵消,即-1表示从最后一个元素开始查找,-2表示从倒数第二个元素开始查找,如果在设置范围内找到则返回0,否则返回-1。

console.log(arr.indexOf('one')); // 0
console.log(arr.indexOf('onef')); // -1

console.log(arr.indexOf('one', 4));// -1
console.log(arr.indexOf('onef', 4));// -1

console.log(arr.indexOf('one', -3)); // 0
console.log(arr.indexOf('one', -10)); // 0
console.log(arr.indexOf('one', -2)); // -1
复制代码
③Array.prototype.lastIndexOf(String,fromIndex) 方法

返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。同②

④Array.prototype.find(function) 方法

返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。

var arr = [5, 12, 8, 130, 44];

var found = arr.find(function(element) {
  return element > 10;
});

var found1 = arr.find(function(element) {
  return element > 200;
});

console.log(found); //12
console.log(found1); //undefined
复制代码
⑤Array.prototype.filter(function) 方法

创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

var arr = [12, 5, 8, 130, 44];
function isBigEnough(element) {
  return element >= 10;
}
var filterArr = arr.filter(isBigEnough);

console.log(filterArr);//[12, 130, 44]
复制代码
3. 依据条件过滤数组中满足条件的数据 ?(涉及ES6箭头函数)

可以根据2问中的函数编写方法过滤。 其中有一个Array.prototype.filter(function)方法 创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

① eg:知道value值的一个数组,根据字典关系数据筛选出对应的text值的数组?

相关知识:map() ?includes()?filter()

//场景
var values = ['1','2','4'];
var data = [{value:'1',text:'清香1'},
            {value:'2',text:'清香2'},
            {value:'3',text:'清香3'},
            {value:'4',text:'清香4'},
            {value:'5',text:'清香5'},
            {value:'6',text:'清香6'}];
            
我需要得到 与values对应的text值的数组
例如:var texts = ['清香1','清香2','清香4'];
复制代码

方法一:

var valueArr = []
data.map(e=>{
    if(values.includes(e.value))valueArr.push(e.text)
})
console.log(valueArr)
复制代码

方法二:(推荐使用⭐⭐⭐⭐)

//用filter过滤,再用map。 includes 是ES7的,可以用indexOf替代。
//使用Array.prototype.filter(function) 方法:
var result = data.filter(e=>{
    return values.includes(e.value)
}).map(el => el.text);
console.log(result); // ["清香1", "清香2", "清香4"]
复制代码

4. 用==还是===好呢?

①== 两边值类型不同的时候,要先进行类型转换,再比较。
②=== 不做类型转换,类型不同的一定不等。

console.log(undefined == null);//true
console.log(undefined === null);//false

console.log(0 == false);//true
console.log(0 === false);//false

console.log(10 == "10");//true
console.log(10 === "10");//false
复制代码

转载于:https://juejin.im/post/5c1cac50f265da61137f409c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值