今天开始和大家一起系统的学习ES6+,每天3分钟,用一把斗地主的时间,重学ES6+,今天介绍的是ES10中新增的内容
相当于数组扁平化,接受一个参数,设置扁平化的层级
// 1.flat的使用
const nums = [10, 20, [2, 9], [[30, 40], [10, 45]], 78, [55, 88]]
const newNums = nums.flat()
console.log(newNums)
//[10, 20, 2, 9, [30,40], [10,45], 78, 55, 88]
const newNums2 = nums.flat(2)
console.log(newNums2)
//[10, 20, 2, 9, 30, 40, 10, 45, 78, 55, 88]
// 2.flatMap的使用
// 可以拆分字符串,把数组的中的短语拆成单词
const messages = ["Hello World", "hello lyh", "my name is coderwhy"]
const words = messages.flatMap(item => {
return item.split(" ")
})
console.log(words)
// ['Hello', 'World', 'hello', 'lyh', 'my', 'name', 'is', 'coderwhy']
const obj = {
name: "yz",
age: 24,
height: 1.88
}
const entries = Object.entries(obj)
console.log(entries)
//[['name', 'yz'], ['age', 24],['height', 1.88]]
const newObj = {}
for (const entry of entries) {
newObj[entry[0]] = entry[1]
}
console.log(newObj)
//{name: 'yz', age: 24, height: 1.88}
// 1.ES10中新增了Object.fromEntries方法
// 可以直接解析类对象数组
const newObj = Object.fromEntries(entries)
console.log(newObj)
//{name: 'yz', age: 24, height: 1.88}
// 2.Object.fromEntries的应用场景
// 解析地址上的参数
const queryString = 'name=yz&age=24&height=1.88'
const queryParams = new URLSearchParams(queryString)
for (const param of queryParams) {
console.log(param)
}
const paramObj = Object.fromEntries(queryParams)
console.log(paramObj)
////{name: 'yz', age: 24, height: 1.88}
const message = " Hello World "
console.log(message.trim())
console.log(message.trimStart())
console.log(message.trimEnd())
最后,这是我第一次参加更文活动,茫茫人海中,如果有幸遇到你,读到我这篇文章,那真是太好了。我深知还有很多不足,希望大家能多提建议,还是想舔着脸皮,向屏幕前的大帅比们,大漂亮们,恳请一个小小的点赞,这会是对我莫大鼓励。也祝愿点赞的大帅比们,大漂亮们升职加薪走向人生巅峰!
阅读量:2013
点赞量:0
收藏量:0