ES6 Learning Notes: Easy understanding of ES6's built-in extended objects

Four ES6-related technologies have been shared earlier. For more information, check out the following connections
ES6 Learning Notes (1): Easy understanding of object-oriented programming, classes and objects)
ES6 Learning Notes (2): Teach you to play with inheritance of classes and objects of classes
<ES6 Learning Notes (3): Teach you to use js object-oriented thinking to add, delete, and change tab columns.
ES6 Learning Notes (4): Teach you to understand the new ES6 grammar

Article Directory

Array's Extension Method

Extension Operator (Expansion Syntax)

Extension operators convert arrays or objects to comma-separated parameter sequences

// Extension operators split arrays into comma-separated parameter sequences
let arr = [1, 2, 3]
console.log(...arr) // 1 2 3

Extension operators can be applied to merged arrays

// Extended Operators Applied to Combination of Numbers
let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
let arr3 = [...arr1, ...arr2]
console.log(arr3) // [1, 2, 3, 4, 5, 6]

// Second method of merging arrays
let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
arr1.push(...arr2)
console.log(arr1) // [1, 2, 3, 4, 5, 6]

Converting an array of classes or traversable objects to a real array using an extension operator

  <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()

Converts an array of classes or traversable objects to a real array

let arrLike = {
	'0': 'a',
  '1': 'b',
  '2': 'c',
  length: 3
}
let arr2 = Array.from(arrayLike) //['a', 'b', 'c']

The method also accepts the second parameter, acting like an array's map method, which processes elements and places the processed values 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]

Instance method: find()

Used to find the first eligible array member, if no undefined return is found

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"}

Instance method: findIndex()

Used to find the location of the first eligible array member, if no Return-1 is found

let arr = [1, 5, 10, 15]
let index = arr.findIndex(value => value > 9)
console.log(index); //2

Instance method: include ()

Returns a Boolean value indicating whether an array contains a given value

[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false

String's Extension Method

Template String

ES6's new way of creating strings, defined with a backboot number
Line breaks are possible in Template Strings
A function can be called in a template string

let name = `zhangsan`
let sayHello = `hello, my name is ${name}`
console.log(sayHello ) // hello, my name is zhangsan
//Line breaks are possible in Template Strings
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)

// A function can be called in a template string
const sayHello = function() {
	return 'hello'
}
let greet = `${sayHello()}, lanfeng`
console.log(greet) //hello, lanfeng

Instance methods: startsWith() and endsWith()

startsWith(): Returns a Boolean value indicating whether the parameter string is at the head of the original string
endsWith(): Returns a Boolean value indicating whether the parameter string is at the end of the original string

let str = 'hello world !'
str.startsWith('hello') // true
str.endsWith('!') //true

Instance method: repeat()

The repeat method means to repeat the original string n times and return 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 is similar to an array, but members have unique values and no duplicate values
Set itself is a constructor for generating Set data structures
The Set function can accept an array as an argument to initialize

const set = new Set([1, 2, 3, 4, 4])
console.log(set.size) // 4. Array Weighting
console.log(set) //Set(4) {1, 2, 3, 4}
//Convert to Array
console.log([...set]) //[1, 2, 3, 4] 	

Instance Method

  • add(value): Add a value and return the Set structure itself
  • delete(value): Deletes a value and returns a Boolean value indicating whether the deletion was successful
  • has(value): Returns a Boolean value indicating whether the value is
  • Members of Set
    clear(): Clear all members, no return value
 const s = new Set();
 s.add(1).add(2).add(3); // Add value to set structure 
 s.delete(2)             // Delete 2 values from set structure 
 s.has(1)                // Indicates whether 1 in the set structure returns a Boolean value 
 s.clear()               // Clear all values in the set structure

ergodic

Instances of the Set structure, like arrays, have a forEach method that performs an action on each member with no return value

const set = new Set([a, b, c])
set.forEach(item => {
	console.log(item)
})

summary

This article mainly shares the usage and examples of Array's extension methods, String's extension methods, Set's data structure, etc. of the built-in extension objects of ES6.

269 original articles were published. 272 were praised. 480,000 visits+
His message board follow

Tags: Programming

Posted on Sun, 08 Mar 2020 22:11:52 -0400 by rxsid