Map and set in JavaScript

Source: Internet
Author: User
Tags delete key

The default object representation of JavaScript {} can be thought of as a map or dictionary data structure in other languages, a set of key-value pairs.

But the JavaScript object has a small problem, that is, the key must be a string , but actually number or other data type as a key is also very reasonable.

Map is the structure of a set of key-value pairs with extremely fast lookup speeds.

var m  new Map ([[' Michael ', 95],[' Bob ', 75],[' Tracy ', []]);        M.get (' Michael '); //  the

Initializing a map requires a two-dimensional array, or directly initializing an empty map. Map has the following methods:

        var New Map (); // Empty map        M.set (' Adam ', 67); // Add a new Key-value        M.set (' Bob ',);        M.has (' Adam '); // is there a key ' Adam ': true        M.get (' Adam '); //  the        M.delete(' Adam '); // Delete key ' Adam '        M.get (' Adam '); // undefined

There is rain a key can only correspond to one value, so, multiple times to a key put value, the following value will overwrite the front

var New Map ();        M.set (' Adam ', ();        M.set (' Adam ',N);        M.get (' Adam '); //  the

Set

Set is similar to a map, but it is also a set of keys, but does not store value, because key cannot be duplicated, so there is no duplicate key in set.

To create a set, you need to provide an array as input, or create an empty set directly:

var New Set (); // Empty Set        var New Set ([n/a]); // contains

Repeating elements are automatically filtered in set:

var New Set ([1,2,3,3, ' 3 ']); s; // set{1,2,3, ' 3 '}, filters out a repeating element 3

You can add elements to the set by using the Add (key) method, but you can add them repeatedly, but it won't work.

>>> S.add (4)        >>>s        {1,2,3,4}        >>> s.add (4)         >>> s        {1,2,3,4}

You can delete an element by using the Delete (key) method:

var New Set ([n/a]);        s; // set{1,2,3}        S.Delete(3);        s; // set{1,2}

Map and set in JavaScript

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.