JSFiddle - React, Tailwind, and code Playground

by cheeken

JavaScript

var Person = function(code, name) {
    this.code = code;
    this.name = name;
};

var PersonArray = function(persons) {
    this.elements = {};
    var i;
    for (i=0; i < persons.length; i++) {
        this.elements[persons[i].code] = persons[i];
    }
};

PersonArray.prototype.fromCode = function(s) {
    return this.elements[s];   
};

var people = new PersonArray([
    new Person("ABC123", "Tooth Fairy"),
    new Person("DEF456", "Santa Claus"),
    new Person("PIR000", "Jack Sparrow"),
    new Person("XYZ987", "Easter Bunny")
    ]);

console.log(people.fromCode("ABC123"));
console.log(people.fromCode("DEF456"));
console.log(people.fromCode("NONE"));