1. Spread OperatorThe spread operator is three dots ... , which allows to expand an expression in place, converting it to a comma-separated sequence of arguments when multiple arguments are required (such as a function call) or multiple values (such as an array). The sample code is as follows: // Define an array let arr = [1, 2, 3, 4, 5, 6] // Expand using the ... spread operator console.log(...arr); // 1 2 3 4 5 6 // Define a function function fun(...item) { console.log(...item); } // Call function fun(1, 2, 3, 4, 5, 6) // 1 2 3 4 5 6 // Use with expressions let x = 10 arr = [ ...(x > 0 ? ['a'] : []), 'b', ]; console.log(...arr); //ab 2. Alternative apply() method Since the spread operator can expand arrays, there is no need for the The sample code is as follows: // define a function function fun(a, b, c, d, e) { console.log(a, b, c, d, e); } // Define an array let arr = [1, 2, 3, 4, 5] // ES5 calling method fun.apply(null, arr) //1 2 3 4 5 // ES6 calling method fun(...arr) // 1 2 3 4 5 If we take out the maximum value in the array in actual development, the method used is as follows: let arr = [1, 20, 30, 50, 3, 88, ] // ES5 let max = Math.max.apply(null, arr) console.log(max); // 88 E is written as follows: let arr = [1, 20, 30, 50, 3, 88, ] // ES6 let max = Math.max(...arr) console.log(max); // 88 3. Application of extension operatorThe application of extended arrays is mainly reflected in the following aspects 1. Copy array Before The sample code is as follows: Let's first understand the concept of deep and shallow copy:
let arr1 = [1, 2, 3, 4, 5] let arr2 = arr1 console.log(arr2); // [ 1, 2, 3, 4, 5 ] // Modify the data content of arr2 arr2[2] = 6; // Both will change console.log(arr1, arr2); // [ 1, 2, 6, 4, 5 ] [ 1, 2, 6, 4, 5 ] If you want to complete a deep copy, the sample code is as follows: let arr1 = [1, 2, 3, 4, 5] let arr2 = [] // ES5 for (let i = 0; i < arr1.length; i++) { arr2[i] = arr1[i]; } arr2[2] = 6; // Only arr2 changes console.log(arr1, arr2); // [ 1, 2, 3, 4, 5 ] [ 1, 2, 6, 4, 5 ] // ES6 arr2 = [...arr1] arr2[2] = 6; // Only arr2 changes console.log(arr1, arr2); // [ 1, 2, 3, 4, 5 ] [ 1, 2, 6, 4, 5 ] 2. Merge arraysThe spread operator provides a new way to write array merging. The sample code is as follows: const arr1 = ['a', 'b']; const arr2 = ['c']; const arr3 = ['d', 'e']; // ES5 merged array console.log(arr1.concat(arr2, arr3)); // [ 'a', 'b', 'c', 'd', 'e' ] // ES6 merged array console.log([...arr1, ...arr2, ...arr3]); // [ 'a', 'b', 'c', 'd', 'e' ]
3. Use with decoupled assignmentThe spread operator can be combined with destructuring assignment to generate arrays (for taking remaining data).
The sample code is as follows: // Scenario analysis: get the first and last values in the array let arr = [1, 2, 3, 4, 5] let first, rest; // ES5 writing: borrow Array.slice() function first = arr[0] rest = arr.slice(1) console.log(first, rest); // 1 [ 2, 3, 4, 5 ] // ES6 way of writing [first, ...rest] = arr console.log(first, rest); // 1 [ 2, 3, 4, 5 ] 4. Split a string into an arrayThe spread operator can also convert strings into true arrays. The sample code is as follows: let str = 'Fox Demon Little Matchmaker' console.log([...str]); // [ 'fox', 'demon', 'little', 'red', 'mother' ] This is the end of this article about the introduction of the new feature of JS ES extension operator. For more information about the introduction of ES extension operator, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: CSS float property diagram float property details
>>: Mysql date formatting and complex date range query
Table of contents cause reason Introduction to NP...
Table of contents 1. Primary key exists 2. No pri...
Table of contents Hbase installation and configur...
Copy code The code is as follows: <html> &l...
DOCTYPE DECLARATION At the top of every page you w...
Table of contents 1. How to obtain elements Get i...
Preface The apt-get command is a package manageme...
Newer Linux distributions no longer have the rc.l...
Table of contents 【Common commands】 [Summary of c...
Static files Nginx is known for its high performa...
1. Analytical thinking 1. Eliminate the machine...
Countdown function needs to be implemented in man...
Latest solution: -v /usr/share/zoneinfo/Asia/Shan...
Transition document address defines a background ...
When using element-ui, there is a commonly used c...