• Match getstr() to object

    From Ephram@VERT/EPHRAM to All on Thu Jun 4 11:53:48 2020
    I have a script that gets a partial string that needs to be matched against an object/array.

    var names = {
    AD:"Andorra",
    AE:"United Arab Emirates",
    AF:"Afghanistan",
    AG:"Antigua and Barbuda",
    AI:"Anguilla", ... };

    write(' \1cEnter Country Name to Lookup \1w');
    var lookup = console.getstr();
    writeln('\r\n \1cLooking up \1w'+ lookup);
    for(var i = 0; i <= names.length; i++) {
    if(names[i][1].toUpperCase() === indexOf(lookup).toUpperCase()) {
    writeln(' Found ' + names[i] + ' ' + names[i][0] + ' ' + names[i][1]);
    }
    }

    if I enter, say lookup = 'Andor' it doesn't find anything ...

    any help would be appreciated :)


    My doctor said I have the body of a 25 year old ... and the mind of a 10 :-/

    ---
    þ Synchronet þ Realm of Dispair BBS - http://ephram.synchro.net:82
  • From echicken@VERT/ECBBS to Ephram on Thu Jun 4 11:23:33 2020
    Re: Match getstr() to object
    By: Ephram to All on Thu Jun 04 2020 11:53:48

    var names = {
    AI:"Anguilla", ... };
    for(var i = 0; i <= names.length; i++) {

    'names' is an Object, but you're treating it like an Array.

    You could try something like:

    for (var name in names) {
    // eg. name === 'AI' and names[name] === 'Anguilla'
    }

    Or something like:

    var keys = Object.keys(names);
    for (var i = 0; i < keys.length; i++) {
    // eg. keys[i] === 'AI' and names[keys[i]] === 'Anguilla'
    }

    Or something like:

    Object.keys(names).forEach(function (e) {
    // eg. e == 'AI' and names[e] === 'Anguilla'
    });

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Ephram@VERT/EPHRAM to echicken on Thu Jun 4 12:51:32 2020
    Re: Match getstr() to object
    By: Ephram to All on Thu Jun 04 2020 11:53:48

    var names = {
    AI:"Anguilla", ... };
    for(var i = 0; i <= names.length; i++) {

    'names' is an Object, but you're treating it like an Array.

    You could try something like:

    for (var name in names) {
    // eg. name === 'AI' and names[name] === 'Anguilla'
    }

    Or something like:

    var keys = Object.keys(names);
    for (var i = 0; i < keys.length; i++) {
    // eg. keys[i] === 'AI' and names[keys[i]] === 'Anguilla'
    }

    Or something like:

    Object.keys(names).forEach(function (e) {
    // eg. e == 'AI' and names[e] === 'Anguilla'
    });

    echicken


    Thank you, I appreciate that!


    My doctor said I have the body of a 25 year old ... and the mind of a 10 :-/

    ---
    þ Synchronet þ Realm of Dispair BBS - http://ephram.synchro.net:82