前言
在es6中我们有很多简单好用的方法,这篇文章将会概述字符串、数组、对象的扩展功能
提示:以下是本篇文章正文内容,下面案例可供参考
一、字符串的扩展功能
1.判断包含关系
- 字符串.includes(子串[,开始位置])
返回值:boolean
let str = 'hello world ni hao shijie';
let res = str.includes('world',10)
console.log(res)
- 字符串.startsWith(子串[,开始位置])
判断子串是否在字符串的开始位置
返回值:boolean
let str='hello word ni hao shijie';
let res=str.startwith('word',6)
console.log(res)
- 字符串.endsWith(子串[,结束位置])
判断子串是否在字符串的结束位置
返回值:boolean
let str='hello word ni hao shijie';
let res=str.startwith('wo',8)
console.log(res)
2.字符串重复次数
字符串.repeat(次数)
注意不支持负数,如果写小数只要整数部分
3.字符串长度补全
- 字符串.padStart(长度[,使用什么字符补全])
let str='abc';
let res=str.padStart(10,'x')
console.log(res)
- 字符串.padEnd(长度[,使用什么字符补全])
let number=122240
如果必须展示九位数
let res=number.toString().padStart('9;,0)
console.log(res)
4.模板字符串
语法:“”/‘’ -> ``
优点:
- 可以保留字符串的格式
- 可以很方便的插入变量-》${变量}
`hello wo ${number} rld`
二、数组的扩展功能
- Array.from(你要转换的数组)
可遍历对象也可以转换成数组 - 将一组值转换成数组
Array.of(值1,值2…)
let arr = Array.of(1,23,3,5)
console.log(arr)
使用Array.of的原因:
使用new Array创建数组,当参数只有一个,并且为数字时,这个参数代表的是长度,而不是数组元素了!
- find()和findIndex()
(1)根据条件找到数组的某元素
数组.find(function(item){
// item 每一次的数组元素
})
如果回调函数返回true,返回的就是item;如果一直没有返回true,结果是undefined
let list = [
{
id:1,
title:'hahahahehehe'
},
{
id:2,
title:'hahahahehehe'
},
{
id:3,
title:'hahahahehehe'
},
]
(2)将id等于2的数组的title修改
let res = list.find(function(item){
return item.id === 2
})
res.title = 'hehehehe';
console.log(list)
- fill ()替换填充
(1)填充替换
let arr=[1,2]
let res=arr.fill(‘a’)
console.log(res)
(2)为空数组填充内容
let arr=new Array(10)
let li=document.createElement(‘li’)
arr.fill(li);
console.log(arr)
(3)使用参数
fill(内容, 开始, 结束)
let arr = new Array(10)
let li = document.createElement('li');
arr.fill(li);
arr.fill('a',2,5);
console.log(arr)
- includes()判断数组是否包含某个元素
let arr=[2,22,2,3,45,5]
let res=arr.includes(5)
console.log(res)
- map()数组加工方法
map会遍历数组然后接收一个回调函数,回调函数的返回值会替换掉你当前的元素
语法:
let res=数组.map(function(item,index,a){
return 值;
})
返回值:新的数组
let list = [
{id:1,name:'苦茶紫',price:25},
{id:2,name:'袜子',price:15},
{id:3,name:'背心',price:45}
]
let newList = list.map(function(item){
item.count = 1;
return item;
})
console.log(newList)
- forEach()数组遍历的方法
list.forEach(function(item,index,a){
console.log(item,index,a)
})
- every()判断数组中的所有元素是否都符合同一个条件
every接收一个回调函数,如果这个回调函数的返回值都是true,则结果为true;
只要出现一次为false,则结果为false
let list = [
{id:1,name:'苦茶紫',price:25},
{id:2,name:'裤子',price:15},
{id:3,name:'背心',price:45}
]
let res = list.every(function(item){
return item.price > 10
})
console.log(res)
- reduce()对数组的元素做累计处理
语法:
无初始值,prev是第一个数组元素
let res=数组.reduce(function(preV,currenV){
return 两个值的计算结果
})
返回值:总的计算结果
有初始值,prev是初始值
let arr=[4,5,6]
let res=arr.reduce(function(preV,currentV,index,a){
return preV+currentV
},10)
console.log(res)
let list = [
{id:1,name:'苦茶紫',price:25},
{id:2,name:'裤子',price:15},
{id:3,name:'背心',price:45}
]
let allP = list.reduce(function(prev,currentv){
return (typeof(prev) === 'number'?prev:prev.price) + currentv.price
})
console.log(allP)
三、对象的扩展功能
- 可以在对象中快速的书写属性和方法
let name='张三'
let obj={
name,
say(){
}
}
- Object.assian(目标对象,源对象1,源对象2…)对象合并
他是把源对象合并到目标对象中
如果遇到同名属性,后边的对象会覆盖前面的对象
深拷贝
let obj = {name:3,sex:3}
let info = {name:2,age:2}
let info1 = {leg:2}
let newObj = Object.assign({},obj,info,info1);
console.log(newObj)
- Object.keys();Object.values();Object.entries()
let info = {
name:'zhagnsan',
age:12,
sex:1
}
let res = Object.keys(info);
let res1 = Object.values(info);
let res2 = Object.entries(info);
console.log(res2)