JSFiddle - React, Tailwind, and code Playground

by Zevan

JavaScript

var d = new Dictionary();

var keyA = {};
var keyB = {};

d(keyA, 1);
d(keyB, 2);
d("ten", 10);
d(10.5, "ten point five");
d(0, "zero");

// delete values
d.delete(keyB);

// loop through values
d.each(function(key) {
    document.write(key + "=>" + d(key) + "<br>");
});

document.write("size : " + d.size());

function Dictionary() {
    var obj = [];

    function find(key) {
        var i = obj.length;
        while (i--) {
            if (obj[i][0] == key) {
                return i;
            }
        }
        return null;
    }

    function d(key, value) {
        if (value) {
            obj.push([key, value]);
        } else {
            var index = find(key);
            if (index != null) {
                return obj[index][1];
            }
        }
    }
    d.size = function() {
        return obj.length;
    }
    d.delete = function(key) {
        obj.splice(find(key), 1);
    }
    d.each = function(func) {
        for (var i = 0; i < obj.length; i++) {
            func(obj[i][0]);
        }
    }
    return d;
}