• Match getstr() to object

    From Ephram@EPHRAM to All on Thursday, June 04, 2020 11:53:48
    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@ECBBS to Ephram on Thursday, June 04, 2020 11:23:33
    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@EPHRAM to echicken on Thursday, June 04, 2020 12:51:32
    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