Traversal of data structures in ES6 and ES5

Source: Internet
Author: User
Tags map data structure

Look at the ES6 to feel a variety of data structure of the traversal method a lot of good confusion, write down the summary, see what the difference in the application scenario

Array:

ES5:

(1) Array.prototype.forEach (function (Item,index,array) {...})

(2) Array.prototype.map (function (Value,index,array) {...//return value, which is inserted into a new array}) is mapped to a new array

(3) Array.prototype.some (the function (item) {...//condition}) an item in the array is satisfied to stop execution and returns true

(4) Array.prototype.every (the function (item) {...//condition}) an item in the array that is not satisfied stops execution and returns false.

(5) Array.prototype.filter (function (item) {...//return true or false}) returns the new filtered array

(6) Array.prototype.indexOf (item)

(7) Array.prototype.lastIndexOf (item)

(8) Array.prototype.reduce (function (Previous, current, index, array) {... return value//returns value as the previous value of the Next loop})

(9) Array.prototype.reduceRight Ibid, but the initial value of index is array.length-1

ES6:

(1) Array.from (parameter), the most used should be to convert the set to an array, or convert the result of the element selector to an array

(2) Array.of (parameter) eliminates overloads of the new Array (parameter) due to different number of parameters

(3) Array.prototype.copyWithin (target, start = 0, end = this.length) I didn't think it would be useful.

(4) Array.prototype.find (function (value, index, arr) {...//condition}) found the first member to return a value of true

(5) Array.prototype.findIndex (function (Value.index,arr) {...//condition}) function as above, return index

(6) Array.prototype.keys () Gets the key name Walker

(7) Array.prototype.values () Gets the key-value Walker

(8) Array.prototype.entries () Gets the key-value pair Walker

Set data structure

When the data structure is updated or created it will go heavy, like = = = but here nan and nan are equal

(1) Set.prototype.add (parameter)

(2) Set.prototype.delete (parameter)

(3) Set.prototype.has (parameter)

(4) Set.prototype.clear ()

(5) Set.prototype.keys () The iterator that returns the key name

(6) Set.prototype.values () returns the key-value Walker

(7) Set.prototype.entries () returns the key-value pair Walker

(8) Set.prototype.forEach (function (value.key,set) {}) traversal

MAP data structure

A collection of key-value pairs, but the key name can be an object, and when the key name is the object, the same memory address is considered the same as the key name

(1) Map.prototype.set (Key,value)

(2) Map.prototype.get (Key)

(3) Map.prototype.has (Key)

(4) Map.prototype.delete (Key)

(5) Map.prototype.clear ()

(6) Map.prototype.keys ()

(7) Map.prototype.values ()

(8) Map.prototype.entries ()

(9) Map.prototype.forEach (function (Value,key,map) {...})

Here you need to pay attention to the map and JSON conversion, specifically can refer to Nanyi's article

Summary, the personal feeling set and array in addition to the weight of no difference, and they can be converted to each other, there is no application of the difference between the scene. The map object is more convenient to traverse than the ES5 object, and the key name can be an object.

Traversal of data structures in ES6 and ES5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.