Create a Map collection in JS

Source: Internet
Author: User

function map () {//Create a map collection

This defines the data structure
var struct = function (key, value) {
This.key = key;
This.value = value;
}

This is the method of adding

var put = function (key, value) {
for (var i = 0; i < this.arr.length; i++) {
if (This.arr[i].key = = = key) {
This.arr[i].value = value;
Return
}
}
This.arr[this.arr.length] = new struct (key, value);
}

Method of getting

var get = function (key) {
for (var i = 0; i < this.arr.length; i++) {
if (This.arr[i].key = = = key) {
return this.arr[i].value;
}
}
return null;
}
This is a method that is defined according to your own needs.
var getindex = function (index) {
if (This.arr.length-1>=index) {
return this.arr[index].key;
}
return null;
}

Method of deletion

var remove = function (key) {
var V;
for (var i = 0; i < this.arr.length; i++) {
v = this.arr.pop ();
if (v.key*1 = = = key*1) {
Continue
}
This.arr.unshift (v);
}
}

Get the number of stores

var size = function () {
return this.arr.length;
}

Determines whether the empty

var isEmpty = function () {
return this.arr.length <= 0;
}
Empty
var tonull=function () {
This.arr=new Array ();
}

Publishing services

This.arr = new Array ();
This.get = get;
This.put = put;
This.tonull=tonull;
This.remove = remove;
This.getindex =getindex;
This.getvalue=getvalue;
this.size = size;
This.isempty = IsEmpty;
}

Create a Map collection in JS

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.