Introduction to the array methods and objects added by ES6, Introduction to the addition of arrays by es6
Es6 will use this method to traverse the array, or the object, as well as the set and map methods.
let arr=[1,2,3,4,3,2,1,2];
The simplest and most straightforward way to traverse Arrays
For (let value of arr) {console. log (value); // output 1, 2, 3, 3, 2, 2}
1. array. map ()
Returns a new array. es5 uses loops to copy a new array.
Let arr = [1, 2, 3, 3, 2, 2]; let newArr = arr. map (value, index, arr) => value) console. log (newArr) // output [1, 2, 3, 4]. Of course, you can also return the console of the underlying array. log (newArr = arr) // outputs false
2. array. filter ()
Filter and return the true value,
Let arr = [1, 2, 3, 3, 2, 2]; let newArr1 = arr. filter (value, index, arr) => value> = 3) console. log (newArr1); // output [3, 4, 3]
3. array. reduce ()
The meaning of each parameter is the returned value or initial value of the previous callback of previusvalue. currentValue is the array value being processed, and currentIndex is processing the subscript of the function.
// We used to find the maximum value. The minimum value was obtained through a 2-layer loop. Now we can get it through a line of code. Is it nice to let arr = [1, 2, 3, 3, 2, 2]? let newArr2 = arr. reduce (pre, cur, curIndex, arr) => pre> cur? Pre: cur) // pre = 1 is not greater than cur = 2, 2 is returned; // pre receives the return value 2; pre = 2 is not greater than cur = 3, returns 3 ..... // always find 4 and return 4; // pre receives 4, pre = 4 is greater than pre = 3 and then always returns 4, so that the maximum console is found. log (newArr2) // output 4. Find the maximum value. If you want to find the minimum value, you only need pre <cur? Pre: cur
Es6 provides a new data structure, which is similar to an array, but the values of members are unique and there are no repeated values (including NaN), but the internal objects of the set are not equal.
If Set is not an array, it naturally does not have the length attribute. It has the size attribute, set. size, which is the number of returned Members;
Let set = new Set ([1, 2, 3, 3, 2, 1]); // The array console must be input here. log (set) // output set {1, 2, 3, 4} console. log (set. add (2) // still output set {1, 2, 3, 4} console. log (set. add (5) // output set {1, 2, 3, 4, 5} console. log (set. delete (1) // Output true, indicating that the console is successfully deleted. log (set. has (2) // outputs true, indicating that the value console exists. log (set. clear () // output undefined. This method deletes all values without returning the console. log (set) // at this time the set has been cleared, so the output is set {}
The Array. from () method creates a new Array instance from an object similar to an Array or can be iterated.
Let set = new Set ([1, 2, 3, 3, 2, 1]); console. log (Array. from (set) // you can use this method to convert the set object to my array and output it to [, 1] console. log (Array. from ('hello') // The output is ['h', 'E', 'l', 'l', 'O']
Array deduplication using set and from
Let arr1 = [1, 2, 1, 3, 5, 2] console. log (Array. from (new Set (arr1) // output [1, 2, 3, 5]. In the future, someone will ask you how to deduplicate the array. you can load it.
Writing it here suddenly comes to mind es6's... Method
console.log([...new Set(arr1)])
Set Traversal method
For (let value of set. keys () {console. log (value) // output key} for (let value of set. values () {console. log (value) // output value} for (let value of set. entries () {console. log (value) // output key-value Pair}
Es6 provides a Map data structure, which is similar to an object and a set of key-value pairs. However, es6 is powerful in that the key range can be any type of data;
Let map = new Map ([["name", "Zhang San"], [[1, 2, 3], 18], [{}, "male"]) console. log (map) console. log (map. set ("name", "") // output Map {'name' => 'zhang san', [1, 2, 3] => 18, {} => 'male'} if this key is not available, a new key-value pair is added to the console. log (map. get ("name") // output Li Si console. log (map. delete ("name") // output trueconsole. log (map. clear () // delete all key-value pairs
The array method and object added in ES6 are all the content shared by Alibaba Cloud xiaobian. I hope you can provide a reference and support for the customer's house.