Because IE8 and the following versions do not support map objects, this article provides an effective solution for the program apes.
This article overrides the map object and implements the usual set, get, put, clear, remove, delete, ForEach, have, ContainsKey, isEmpty, size, and other methods, using and declaring the same as the normal declaration of the Map object:
var map = new map ();
Simply copy the code below into the <script type= "Text/javascript" ></script>.
The code is provided by the Code tool Net (http://matools.com), the code for implementing the Map object is as follows, for reference only:
functionMap () { This. elements =NewArray (); //get the number of map elements This. Size =function() { return This. Elements.length; }, //determine if the map is empty This. IsEmpty =function() { return( This. elements.length < 1); }, //Delete all elements of map This. Clear =function() { This. elements =NewArray (); }, //adding elements to a map (key, value) This. put =function(_key, _value) {if( This. ContainsKey (_key) = =true) { if( This. Containsvalue (_value)) { if( This. Remove (_key) = =true) { This. Elements.push ({key: _key, Value: _value }); } } Else { This. Elements.push ({key: _key, Value: _value}); } } Else { This. Elements.push ({key: _key, Value: _value}); } }, //adding elements to a map (key, value) This. Set =function(_key, _value) {if( This. ContainsKey (_key) = =true) { if( This. Containsvalue (_value)) { if( This. Remove (_key) = =true) { This. Elements.push ({key: _key, Value: _value }); } } Else { This. Elements.push ({key: _key, Value: _value}); } } Else { This. Elements.push ({key: _key, Value: _value}); } }, //deletes the element of the specified key, returns true successfully, and fails to return false This. remove =function(_key) {varbln =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; } returnbln; }, //deletes the element of the specified key, returns true successfully, and fails to return false This.Delete=function(_key) {varbln =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; } returnbln; }, //gets the element value of the specified key, and the failure returns null 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; } }, //set Specifies the element value of key This. SetValue =function(_key, _value) {varbln =false; Try { for(i = 0; i < This. elements.length; i++) { if( This. Elements[i].key = =_key) { This. Elements[i].value =_value; return true; } } } Catch(e) {bln=false; } returnbln; }, //gets the element of the specified index (using Element.key,element.value to get the key and value), and fails to return null This. Element =function(_index) {if(_index < 0 | | _index >= This. Elements.length) { return NULL; } return This. Elements[_index]; }, //determine if the map contains an element of the specified key This. ContainsKey =function(_key) {varbln =false; Try { for(i = 0; i < This. elements.length; i++) { if( This. Elements[i].key = =_key) {bln=true; } } } Catch(e) {bln=false; } returnbln; }, //determine if the map contains an element of the specified key This. has =function(_key) {varbln =false; Try { for(i = 0; i < This. elements.length; i++) { if( This. Elements[i].key = =_key) {bln=true; } } } Catch(e) {bln=false; } returnbln; }, //Determines whether the map contains an element of the specified value This. Containsvalue =function(_value) {varbln =false; Try { for(i = 0; i < This. elements.length; i++) { if( This. Elements[i].value = =_value) {bln=true; } } } Catch(e) {bln=false; } returnbln; }, //gets an array of all keys in the map (array) This. Keys =function() { vararr =NewArray (); for(i = 0; i < This. elements.length; i++) {Arr.push ( This. Elements[i].key); } returnarr; }, //get an array of all the value in the map (array) This. Values =function() { vararr =NewArray (); for(i = 0; i < This. elements.length; i++) {Arr.push ( This. Elements[i].value); } returnarr; }; /** * Map Traversal array * @param callback [function] callback function; * @param context [object] context;*/ This. ForEach =functionForEach (callback,context) {context= Context | |window; //Ie6-8 Write your own callback function execution Logic varNewary =NewArray (); for(vari = 0; I < This. elements.length;i++) { if(typeofcallback = = = ' function ') { varval = Callback.call (Context, This. Elements[i].value, This. Elements[i].key, This. elements); Newary.push ( This. Elements[i].value); } } returnnewary; } }
Custom implement JavaScript Map object, modify IE incompatible map () problem