JavaScript object Array,Map,Set use

Keywords: Javascript IE Java

for (int i = 0; I < 3; I + +) {/ / [focus on three times]
Before we explain the usage of each object, we must pay attention to the compatibility of the browser when using JavaScript objects! Especially the version of IE!!!!
}

To view the specific API, please check JavaScript object Here is the specific API introduction!

Please refer to the above JavaScript object for details of array, Map and set! Let's talk about Map!

Map

JavaScript map is not very easy to use, and there are few API s. There are many similar map implementations on the Internet, which are similar to JAVA map! Here is a map object that your project is using!

/*
 * MAP Object to implement MAP function
 *
 * Interface:
 * size()                Get the number of MAP elements
 * isEmpty()             Judge whether MAP is empty
 * clear()               Delete all MAP elements
 * put(key, value)       Add the element (key, value) to MAP. If the element key exists, delete and add the element (key, value)
 * remove(key)           Delete the element of the specified KEY, return True for success and False for failure
 * get(key)              Get the element VALUE value of the specified KEY, NULL returned in failure
 * element(index)        Get the element of the specified index (use element.key, element.value to get KEY and VALUE). NULL is returned in failure
 * containsKey(key)      Determine whether the MAP contains the element with the specified KEY
 * containsValue(value)  Determine whether the MAP contains the element of the specified VALUE
 * values()              Get ARRAY of all values in MAP (ARRAY)
 * keys()                Get ARRAY of all keys in MAP (ARRAY)
 *
 * example:
 * var map = new Map();
 *
 * map.put("key", "value");
 * var val = map.get("key")
 * ......
 *
 */
function Map() {
    this.elements = new Array();

    //Get the number of MAP elements
    this.size = function() {
        return this.elements.length;
    }

    //Judge whether MAP is empty
    this.isEmpty = function() {
        return (this.elements.length < 1);
    }

    //Delete all MAP elements
    this.clear = function() {
        this.elements = new Array();
    }

    //Add the element (key, value) to the MAP. If the element key exists, delete it and then add the element (key, value)
    this.put = function(_key, _value) {
        var isKey = this.containsKey(_key);
        if(isKey == true)
        {
            this.remove(_key);
        }

        this.elements.push( {
            key : _key,
            value : _value
        });
    }

    //Delete the element of the specified KEY, return True for success and False for failure
    this.remove = function(_key) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key) {
                    this.elements.splice(i, 1);
                    return true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    }

    //Get the element VALUE value of the specified KEY, NULL returned in failure
    this.get = function(_key) {
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key) {
                    return this.elements[i].value;
                }
            }
        } catch (e) {
            return null;
        }
    }

    //Get the element of the specified index (use element.key, element.value to get KEY and VALUE). NULL is returned in failure
    this.element = function(_index) {
        if (_index < 0 || _index >= this.elements.length) {
            return null;
        }
        return this.elements[_index];
    }

    //Determine whether the MAP contains the element of the specified KEY
    this.containsKey = function(_key) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key) {
                    bln = true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    }

    //Determine whether the MAP contains the element of the specified VALUE
    this.containsValue = function(_value) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].value == _value) {
                    bln = true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    }

    //Get ARRAY of all values in MAP (ARRAY)
    this.values = function() {
        var arr = new Array();
        for (i = 0; i < this.elements.length; i++) {
            arr.push(this.elements[i].value);
        }
        return arr;
    }

    //Get ARRAY of all keys in MAP (ARRAY)
    this.keys = function() {
        var arr = new Array();
        for (i = 0; i < this.elements.length; i++) {
            arr.push(this.elements[i].key);
        }
        return arr;
    }
}

attach
Detailed discussion on js traversal set (Array,Map,Set)
http://www.jb51.net/article/110487.htm


If you think this blog post is helpful to you, please like it. Thank you!


If you are handsome (beautiful), wise (intelligent), as simple and kind as I am, you see the problems in this blog post, please point out that I accept the criticism that you let me grow modestly, thank you for reading!
Have a good day!

Welcome to visit my csdn blog, we grow together!

"No matter what you do, as long as you stick to it, you will see something different! On the road, not lowly not high! "

Blog homepage: http://blog.csdn.net/u010648555

Posted by Shane10101 on Mon, 04 May 2020 20:38:42 -0700