当前位置:网站首页>ES6 learning notes (5): easy to understand ES6's built-in extension objects
ES6 learning notes (5): easy to understand ES6's built-in extension objects
2020-11-06 20:37:00 【Tell me Zhan to hide】
I shared four articles about ES6 Related technologies , If you want to know more , You can see the following links
《ES6 Learning notes ( One ): Easy to understand object-oriented programming 、 Classes and objects 》
《ES6 Learning notes ( Two ): Teach you how to play with class inheritance and class objects 》
《ES6 Learning notes ( 3、 ... and ): Teach you how to use js Object oriented thinking to achieve tab Column add, delete, modify and search function 》
《ES6 Learning notes ( Four ): Teach you to understand ES6 New syntax for 》
List of articles
Array Extension method of
Extension operator ( Expand grammar )
Extension operators can convert arrays or objects into a comma separated sequence of parameters
// The extension operator splits an array into a comma separated sequence of parameters
let arr = [1, 2, 3]
console.log(...arr) // 1 2 3
Extension operators can be applied to merge arrays
// Extension operators are applied to array merges
let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
let arr3 = [...arr1, ...arr2]
console.log(arr3) // [1, 2, 3, 4, 5, 6]
// The second way to merge arrays
let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
arr1.push(...arr2)
console.log(arr1) // [1, 2, 3, 4, 5, 6]
The extension operator is used to convert a class array or traversable object into a real array
<div>1</div>
<div>2</div>
<div>3</div>
let oDivs = document.getElementsByTagName('div')
console.log(oDivs) //HTMLCollection(3) [div, div, div]
const arr = [...oDivs];
console.log(arr) //(3) [div, div, div]
Constructor method : Array.from()
Convert an array of classes or traversable objects into a real array
let arrLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
}
let arr2 = Array.from(arrayLike) //['a', 'b', 'c']
Method can also accept the second parameter , Act like an array map Method , Used to process elements , Put the processed value into the returned array .
let arrLike = {
'0': 1,
'1': 2,
'2': 3,
length: 3
}
let arr2 = Array.from(arrLike, item => item * 2)
console.log(arr2) //(3) [2, 4, 6]
Example method :find()
To find the first qualified member of the array , If no return is found undefined
let arr = [{
id: 1,
name: 'lanfeng'
},
{
id: 2,
name: 'qiuqiu'
}
];
let target = arr.find((item, index) => item.id === 2)
console.log(target) //{id: 2, name: "qiuqiu"}
Example method : findIndex()
Used to find the location of the first eligible array member , If no return is found -1
let arr = [1, 5, 10, 15]
let index = arr.findIndex(value => value > 9)
console.log(index); //2
Example method : includes()
Indicates whether an array contains a given value , Returns a Boolean value
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false
String Extension method of
Template string
ES6 New ways to create strings , Use the anti boot number to define
The template string can wrap
Function can be called in template string
let name = `zhangsan`
let sayHello = `hello, my name is ${ name}`
console.log(sayHello ) // hello, my name is zhangsan
// The template string can wrap
let result = {
name: 'zhangsan',
age: 20,
sex: ' male '
}
let html=`<div> <span>${ result.name}</span> <span>${ result.age}</span> <span>${ result.sex}</span> </div>`;
console.log(html)
// Function can be called in template string
const sayHello = function() {
return 'hello'
}
let greet = `${ sayHello()}, lanfeng`
console.log(greet) //hello, lanfeng
Example method : startsWith() and endsWith()
startsWith(): Indicates whether the parameter string is in the header of the original string , Returns a Boolean value
endsWith(): Indicates whether the parameter string is at the end of the original string , Returns a Boolean value
let str = 'hello world !'
str.startsWith('hello') // true
str.endsWith('!') //true
Example method :repeat()
repeat Method to repeat the original string n Time , Returns a new string
const str1 = 'x'.repeat(3)
console.log(str1) // xxx
const str2 = 'hello'.repeat(2)
console.log(str2) // hellohello
Set data structure
ES6 Provides a new data structure Set. It's like an array , But the values of the members are unique , There is no repetition value
Set Itself is a constructor , Used to generate Set data structure
Set Function can take an array as an argument , Used to initialize
const set = new Set([1, 2, 3, 4, 4])
console.log(set.size) // 4 Array weight removal
console.log(set) //Set(4) {1, 2, 3, 4}
// Convert to array
console.log([...set]) //[1, 2, 3, 4]
Example method
- add(value): Add a value , return Set Structure itself
- delete(value): Delete a value , Returns a Boolean value , Indicates whether the deletion is successful
- has(value): Returns a Boolean value , Indicates whether the value is
- Set Members of
clear(): Clear all members , no return value
const s = new Set();
s.add(1).add(2).add(3); // towards set Add value to structure
s.delete(2) // Delete set The structure of the 2 value
s.has(1) // Express set Whether there is... In the structure 1 This value Returns a Boolean value
s.clear() // eliminate set All values in the structure
Traverse
Set An instance of a structure is the same as an array , Also have forEach Method , Used to perform some kind of operation on each member , no return value
const set = new Set([a, b, c])
set.forEach(item => {
console.log(item)
})
summary
This article mainly shares ES6 Built in extension object of Array Extension method of 、String Extension method of 、Set Data structure and other aspects of the use of some methods and examples .
版权声明
本文为[Tell me Zhan to hide]所创,转载请带上原文链接,感谢
边栏推荐
- python100例項
- 大道至简 html + js 实现最朴实的小游戏俄罗斯方块
- Discussion on the development practice of aspnetcore, a cross platform framework
- Use modelarts quickly, zero base white can also play AI!
- 01. SSH Remote terminal and websocket of go language
- What course of artificial intelligence? Will it replace human work?
- 前端未來趨勢之原生API:Web Components
- 代码生成器插件与Creator预制体文件解析
- 解决 WPF 绑定集合后数据变动界面却不更新的问题
- For a while, a dynamic thread pool was created, and the source code was put into GitHub
猜你喜欢
意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
Behind the first lane level navigation in the industry
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
检测证书过期脚本
Gather in Beijing! The countdown to openi 2020
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
CloudQuery V1.2.0 版本发布
仅用六种字符来完成Hello World,你能做到吗?
How to hide part of barcode text in barcode generation software
百万年薪,国内工作6年的前辈想和你分享这四点
随机推荐
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
常用SQL语句总结
Chainlink brings us election results into blockchain everipedia
游戏开发中的新手引导与事件管理系统
代码重构之法——方法重构分析
MongoDB与SQL常用语法对应表
開源一套極簡的前後端分離專案腳手架
【應用程式見解 Application Insights】Application Insights 使用 Application Maps 構建請求鏈路檢視
What if the front end doesn't use spa? - Hacker News
C語言I部落格作業03
Asp.Net Core learning notes: Introduction
Elasticsearch Part 6: aggregate statistical query
How to demote domain controllers and later in Windows Server 2012
消息队列(MessageQueue)-分析
Who says cat can't do link tracking? Stand up for me
For a while, a dynamic thread pool was created, and the source code was put into GitHub
MeterSphere开发者手册
给字节的学姐讲如何准备“系统设计面试”
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
游戏主题音乐对游戏的作用