当前位置:网站首页>ES6-新增方法
ES6-新增方法
2022-07-27 16:21:00 【qq_42042158】
一、includes
判断字符串中是否含有某些字符,或数组是否包含某个元素
1、基本用法
console.log('abc'.includes('a'));//true
console.log('abc'.includes('d'));//false
console.log([1,2,3].includes(1));//true
console.log([1,2,3].includes('1'));//false
2、第二个参数
表示开始搜索的位置,默认是0
console.log('abc'.includes('a',1));//false
二、padStart()和padEnd()
补全字符串长度
1、基本用法
console.log('x'.padStart(5,'ab'));//ababx
console.log('x'.padEnd(5,'ab'));//xabab
2、注意事项
- 原字符串的长度,等于或大于最大长度,不会消减原字符串,字符串补全不生效,返回原字符串
console.log('xxx'.padEnd(2,'ab'));//xxx
- 用来补全的字符串与原字符串长度之和超过了最大长度,截去超出位数的补全字符串,原字符串不动
console.log('abc'.padStart(10, '0123456789'));//0123456abc
- 如果省略第二个参数,默认使用空格补全长度
三、trimStart()和trimEnd()
清除字符串的首或尾空格,中间的空格不会清除
四、数组的Array.from()
将其他数据类型转换成数组
所有可遍历的
数组、字符串、Set、Map、riodeList、arguments
拥有length属性的任意对象
1、基本用法
console.log(Array.from('str'));//[ 's', 't', 'r' ]
const obj = {
'0':'a',
'1':'b',
name: 'Alex',
length: 3
};
console.log(Array.from(obj));//[ 'a', 'b', undefined ]
2、第二个参数
作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组
console.log(
[1,2].map(value => {
return value * 2})
);//[ 2, 4 ]
console.log(Array.from('12', value => value * 2));//[ 2, 4 ]
五、find()和findIndex()
find():找到满足条件的一个立即返回
findIndex():找到满足条件的一个,立即返回某索引
1、基本用法
console.log(
[1,5,10,15].find((value) =>{
return value > 9;
})
);//10
console.log(
[1,5,10,15].findIndex((value) =>{
return value > 9;
})
);//2
六、Object.assign()
合并对象
相同的属性后面的会覆盖前面的
可以合并多个对象
const apple = {
color: '红色',
shape: '圆形',
taste: '甜'
};
const pen = {
color: '黑色',
shape: '圆柱形',
use: '写字'
};
console.log(Object.assign(apple,pen));
//{ color: '黑色', shape: '圆柱形', taste: '甜', use: '写字' }
console.log(apple)//{ color: '黑色', shape: '圆柱形', taste: '甜', use: '写字' }
console.log(Object.assign(apple,pen)===apple)//true
七、Object.keys). Object.values()和Object.entries()
获取对象的键值
const person = {
name: 'Alex',
age: 18
};
console.log(Object.keys(person));//[ 'name', 'age' ]
console.log(Object.values(person));//[ 'Alex', 18 ]
console.log(Object.entries(person));//[ [ 'name', 'Alex' ], [ 'age', 18 ] ]
边栏推荐
- Arrays and objects in JS
- [NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
- Intelligent insomnia therapeutic instrument product dlt8p68sa Jericho
- 低噪负离子风扇触摸IC
- RuntimeError: output with shape [1, 256, 256] doesn‘t match the broadcast shape [3, 256, 256]【报错】
- C static method and non static method
- 连接查询和子查询
- 全自动吸奶器芯片-DLTAP703SD
- MySQL 01 relational database design
- 百度地图鹰眼轨迹服务
猜你喜欢

"MySQL things" explains the indexing principle in detail

Commodity comment information and comment information classification

Led learning eye protection table lamp touch chip-dlt8t10s-jericho

LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创

LeetCode 刷题 第三天

Redis注解

JDBC MySQL 01 JDBC operation MySQL (add, delete, modify and query)

连接查询和子查询

Low noise anion fan touch IC

Vue uses keep alive to realize page caching
随机推荐
JS to achieve smooth scrolling of pages or DOM elements
微信支付及支付回调
express get/post/delete...请求
智能失眠治疗仪产品-DLT8P68SA-杰力科创
LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
Bathroom with demister vanity mirror touch chip-dlt8t10s
Acquisition data transmission mode and online monitoring system of vibrating wire wireless acquisition instrument for engineering instruments
Baidu map eagle eye track service
Collection of software design suggestions of "high cohesion and low coupling"
LeetCode 刷题 第三天
飞机大战敌机出场
MySQL create event execution task
Day 3 of leetcode question brushing
商品评论信息与评论信息分类
Leetcode first day of question brushing
Aircraft battle with enemy aircraft
Alibaba architects spent 280 hours sorting out 1015 pages of distributed full stack pamphlets to easily start the distributed system
npm 基本使用
兆骑科创海内外引进高层次人才,创新创业项目对接
Typeerror: conv2d(): argument 'padding' (position 5) must be multiple of ints, not STR [error]