• Add and object to an array

    From Mortifis@VERT/ALLEYCAT to All on Thursday, July 18, 2019 17:04:09
    Trying to do a list type array that is a list of objects. ie
    const dup_user = {
    number: '',
    alias: '',
    num: '',
    email: '',
    dup_alias: '',
    dup_num: '',
    dup_email: ''
    }

    var user_list = []; // ??

    I'd like to populate by running a nested loop that compares users names: lastuser=system.lastuser;

    for(i=1; i<=lastuser; i++) // loop through users and grab the name
    {
    u = new User(i);

    if(u.settings&(USER_DELETED|USER_INACTIVE))
    continue;

    // start new new loop and compare name
    for(n = i+1; n <= lastuser; n++)
    {
    d = new User(n)
    if(u.number == d.number) continue;
    if(d.settings&(USER_DELETED|USER_INACTIVE)) continue;

    if(u.name == d.name)
    {
    ... do some stuff and add the values to an array
    that can be accessed by the index [x] later on
    is there a push.dup_user(user_list) type expression?

    }
    }



    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From echicken@VERT/ECBBS to Mortifis on Thursday, July 18, 2019 16:14:56
    Re: Add and object to an array
    By: Mortifis to All on Thu Jul 18 2019 17:04:09

    var user_list = []; // ??
    d = new User(n)
    if(u.number == d.number) continue; if(d.settings&(USER_DELETED|USER_INACTIVE)) continue;
    if(u.name == d.name)
    {
    ... do some stuff and add the values to an array
    that can be accessed by the index [x] later on
    is there a push.dup_user(user_list) type expression?
    }

    user_list.push({
    number: d.number,
    alias: d.alias,
    ...
    });

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Mortifis@VERT/ALLEYCAT to echicken on Thursday, July 18, 2019 17:40:34
    Re: Add and object to an array
    By: Mortifis to All on Thu Jul 18 2019 17:04:09

    var user_list = []; // ??
    d = new User(n)
    if(u.number == d.number) continue; if(d.settings&(USER_DELETED|USER_INACTIVE)) continue;
    if(u.name == d.name)
    {
    ... do some stuff and add the values to an array
    that can be accessed by the index [x] later on
    is there a push.dup_user(user_list) type expression?
    }

    user_list.push({
    number: d.number,
    alias: d.alias,
    ...
    });

    Thank you ... and to access it later:

    for (var i = 0; i < user_list.length; i++) {
    print(user_list[i]+"\r\n");
    }

    ??



    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From echicken@VERT/ECBBS to Mortifis on Thursday, July 18, 2019 17:21:11
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 17:40:34

    for (var i = 0; i < user_list.length; i++) {
    print(user_list[i]+"\r\n");
    }

    Then you'd get a whole bunch of [object Object] output or something similar (you're trying
    to print each array element, as a string, but they're objects). Something like this:

    print(user_list[i].alias + '\r\n');

    or this:

    print(JSON.stringify(user_list[i]));

    or this, without the for loop:

    print(JSON.stringify(user_list));

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Mortifis@VERT/ALLEYCAT to echicken on Thursday, July 18, 2019 18:30:52
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 17:40:34

    for (var i = 0; i < user_list.length; i++) { print(user_list[i]+"\r\n");
    }

    Then you'd get a whole bunch of [object Object] output or something similar (you're trying
    to print each array element, as a string, but they're objects). Something like this:

    print(user_list[i].alias + '\r\n');

    or this:

    print(JSON.stringify(user_list[i]));

    or this, without the for loop:

    print(JSON.stringify(user_list));

    excellent, thank you. And, yes, that's what I was getting [object Object] I tried a bunch of methods I researched but none of them worked. still not sure how to get console.log() to work, says undefined.




    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From echicken@VERT/ECBBS to Mortifis on Thursday, July 18, 2019 17:53:01
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 18:30:52

    I tried a bunch of methods I researched but none of them worked. still not sure how to get console.log() to work, says undefined.

    console.log() is a thing in web browsers and in node.js, but not in Synchronet's JS
    environment. Some JS examples you see on the web won't transfer perfectly into this space.

    We do have the log(level, message) function here:

    log(LOG_DEBUG, 'This is a debug message');

    or you can omit the loglevel parameter:

    log('This is a message, probably at the INFO level (?)');

    The LOG_* level variables are defined in sbbsdefs.js.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Nightfox@VERT/DIGDIST to Mortifis on Thursday, July 18, 2019 15:01:19
    Re: Add and object to an array
    By: Mortifis to All on Thu Jul 18 2019 05:04 pm

    var user_list = []; // ??

    ... do some stuff and add the values to an array
    that can be accessed by the index [x] later on
    is there a push.dup_user(user_list) type expression?

    If I understand you, do you want to add/append to the user_list array? You should be able to do this:

    user_list.push(dup_user);

    You can also append literal objects:

    user_list.push({
    number: '',
    alias: '',
    num: '',
    email: '',
    dup_alias: '',
    dup_num: '',
    dup_email: ''
    });

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Mortifis@VERT/ALLEYCAT to Nightfox on Thursday, July 18, 2019 20:14:00
    Re: Add and object to an array
    By: Mortifis to All on Thu Jul 18 2019 05:04 pm

    var user_list = []; // ??

    ... do some stuff and add the values to an array
    that can be accessed by the index [x] later on
    is there a push.dup_user(user_list) type expression?

    If I understand you, do you want to add/append to the user_list array? You should be able to do this:

    user_list.push(dup_user);

    You can also append literal objects:

    user_list.push({
    number: '',
    alias: '',
    num: '',
    email: '',
    dup_alias: '',
    dup_num: '',
    dup_email: ''
    });


    Thank you, I appreciate it.



    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From Mortifis@VERT/ALLEYCAT to echicken on Thursday, July 18, 2019 20:16:09
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 18:30:52

    I tried a bunch of methods I researched but none of them worked. still not sure how to get console.log() to work, says undefined.

    console.log() is a thing in web browsers and in node.js, but not in Synchronet's JS
    environment. Some JS examples you see on the web won't transfer perfectly into this space.

    We do have the log(level, message) function here:

    log(LOG_DEBUG, 'This is a debug message');

    or you can omit the loglevel parameter:

    log('This is a message, probably at the INFO level (?)');

    I saw that, I had thought that writes to a file in /sbbs/data/logs/




    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From Digital Man@VERT to Mortifis on Thursday, July 18, 2019 16:30:27
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 08:16 pm

    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 18:30:52

    I tried a bunch of methods I researched but none of them worked. still not sure how to get console.log() to work, says undefined.

    console.log() is a thing in web browsers and in node.js, but not in Synchronet's JS
    environment. Some JS examples you see on the web won't transfer perfectly into this space.

    We do have the log(level, message) function here:

    log(LOG_DEBUG, 'This is a debug message');

    or you can omit the loglevel parameter:

    log('This is a message, probably at the INFO level (?)');

    I saw that, I had thought that writes to a file in /sbbs/data/logs/

    That depends. It goes where log output goes which may be to the BBS's local console, to windows in the Synchronet Control Panel, to files, to the Windows NT event viewer, to *nix syslog (and whatever it does with it) - so yeah, log() messages can go all kinds of places.

    digital man

    Synchronet/BBS Terminology Definition #42:
    ISDN = Integrated Services Digital Network
    Norco, CA WX: 83.5øF, 50.0% humidity, 14 mph E wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From echicken@VERT/ECBBS to Mortifis on Thursday, July 18, 2019 19:50:51
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 20:16:09

    I saw that, I had thought that writes to a file in /sbbs/data/logs/

    It goes wherever your log output goes (which varies with how your BBS is set up).

    Where are you hoping to see these messages appear?

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Mortifis@VERT/ALLEYCAT to echicken on Friday, July 19, 2019 09:08:39
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 20:16:09

    I saw that, I had thought that writes to a file in /sbbs/data/logs/

    It goes wherever your log output goes (which varies with how your BBS is set up).

    Where are you hoping to see these messages appear?

    just standard stdout (the screen) to generate a menu.



    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From Mortifis@VERT/ALLEYCAT to echicken on Friday, July 19, 2019 09:47:27
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 20:16:09

    I saw that, I had thought that writes to a file in /sbbs/data/logs/

    It goes wherever your log output goes (which varies with how your BBS is set up).

    Bleh, ... backstory, I gotr hurt at work and am trying to learn new stuffies so I chose JS ... I know how to do these things with php ... anyway, it seems the JS examples and such are a little different in sbbs js (JavaScript-C 1.8.5)?

    So, I used: dup_list.push({number: u.number, num: u.number, alias: u.alias, email: u.netmail, d_num: d.number, d_alias: d.alias});

    and thought it could be accessed with a console prompt

    if(js.global.console)
    edit=console.getstr(70,K_LINE);
    else
    edit=readln();

    if(edit=="") {
    print("\r\n\r\nExiting ...\r\n\r\n");
    exit();
    }

    printf("Loading Record " + edit);

    const dupUser = dup_list.find(dup_list => dup_list.number === edit);

    but I get a syntax error :-/

    No wonder I keep going back to php lol


    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From echicken@VERT/ECBBS to Mortifis on Friday, July 19, 2019 09:56:04
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Fri Jul 19 2019 09:08:39

    Where are you hoping to see these messages appear?

    just standard stdout (the screen) to generate a menu.

    write() and writeln() should work anywhere IIRC. If you're running your script under jsexec these are
    what you'll want (along with prompt, confirm, and deny).

    We do have a 'console' object, which represents the remote terminal in a telnet/rlogin/ssh session, and
    is not to be confused with the console object that exists in other JS environments (browser, node.js).
    The console.putmsg() method is generally the best thing to use if you want to send a string to the remote
    terminal.

    As always, see http://nix.synchro.net/jsobjs.html for details on what's available.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From echicken@VERT/ECBBS to Mortifis on Friday, July 19, 2019 11:10:08
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Fri Jul 19 2019 09:47:27

    const dupUser = dup_list.find(dup_list => dup_list.number === edit);

    Fat-arrow functions and implicit return are ES6 features, while we're still on 5.1 here. This means that
    you have to spell out 'function () {}' in full, and you must explicitly return a value.

    This won't work, but to illustrate the equivalent syntax:

    const dupUser = dup_list.find(function (e) {
    return e.number === edit;
    });

    However our Array object does not have a 'find' method, which is also new, so that's a no-go.

    There are other ways to achieve this:

    var dupUser;
    for (var n = 0; n < dup_list.length; n++) {
    if (dup_list[n].number !== edit) continue;
    dupUser = dup_list[n];
    break;
    }

    Or:

    var dupUser;
    dup_list.some(function (e) {
    if (e.number !== edit) return false;
    dupUser = e;
    return true;
    });

    Or you could put a polyfill at the top of your script:

    Array.prototype.find = function (f) {
    var ret;
    var self = this;
    this.some(function (e, i) {
    if (!f(e)) return false;
    ret = self[i];
    return true;
    });
    return ret;
    }

    And then 'find' would be available on Array and my "this won't work" example above would actually work.

    In any of the above cases, dupUser will either be the element from your dup_list array, or it will be
    undefined.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Mortifis@VERT/ALLEYCAT to echicken on Saturday, July 20, 2019 14:03:15
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 20:16:09

    I saw that, I had thought that writes to a file in /sbbs/data/logs/

    It goes wherever your log output goes (which varies with how your BBS is set

    Hmmm ... I used log(LOG_INFO,"dup_user_check: (" + dateTime + ") " + msg);
    but I do not see the entry in any /sbbs/data/logs/*.log file :/



    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81
  • From echicken@VERT/ECBBS to Mortifis on Saturday, July 20, 2019 14:07:45
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Sat Jul 20 2019 14:03:15

    Hmmm ... I used log(LOG_INFO,"dup_user_check: (" + dateTime + ") " + msg); but I do not see the entry in any /sbbs/data/logs/*.log file :/

    I think in the case of something running under jsexec it'll just show up on stdout. Can't remember.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Digital Man@VERT to Mortifis on Saturday, July 20, 2019 11:40:47
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Sat Jul 20 2019 02:03 pm

    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 20:16:09

    I saw that, I had thought that writes to a file in /sbbs/data/logs/

    It goes wherever your log output goes (which varies with how your BBS is set

    Hmmm ... I used log(LOG_INFO,"dup_user_check: (" + dateTime + ") " + msg); but I do not see the entry in any /sbbs/data/logs/*.log file :/

    log() output from jsexec goes to stdout by default. But there are command-line options to control where the console output from jsexec goes.


    digital man

    This Is Spinal Tap quote #18:
    Sustain, listen to it. Don't hear anything. You would though were it playing. Norco, CA WX: 75.5øF, 60.0% humidity, 0 mph NW wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Mortifis@VERT/ALLEYCAT to Digital Man on Saturday, July 20, 2019 16:14:59
    Re: Re: Add and object to an array
    By: Mortifis to echicken on Sat Jul 20 2019 02:03 pm

    Re: Re: Add and object to an array
    By: Mortifis to echicken on Thu Jul 18 2019 20:16:09

    I saw that, I had thought that writes to a file in /sbbs/data/logs/

    It goes wherever your log output goes (which varies with how your BBS is set

    Hmmm ... I used log(LOG_INFO,"dup_user_check: (" + dateTime + ") " + msg); but I do not see the entry in any /sbbs/data/logs/*.log file :/

    log() output from jsexec goes to stdout by default. But there are command-line options to control where the console output from jsexec goes.

    Okay, thank you, I hacked a function mylog() { f = new File(logFile); ... } into my script for now.



    My teachers always said "You can't make a living looking out a window!", they were wrong, I drive truck :-P

    ---
    þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81