Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 5 of 5

Thread: Finding Last Occurrance: lastIndexOf()

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    22

    Default Finding Last Occurrance: lastIndexOf()

    Does anyone know anything about this lastIndexOf function? I'm trying to extract the last level in the generatedcats field in the kmdocument table. The data looks something like this:

    Infrastructure SOPs > Data Center ... or ...
    Infrastructure SOPs > End User Svc > NHD

    I only want the last level, no matter how many levels it is. Looking thru the Java Script Basics book, I found the function lastIndexOf(). According to the book (and several sites), it indexes for the last occurrence of whatever character you provide.

    When I try to use it, I get the following error:
    Script 'KMGenCatsStripper' line 6: ERROR TypeError: $gencats.lastIndexOf is not a function at char 1

    Here is my code:
    =====================================
    function KMLastLevelCat(record)
    {
    var $gencats = record.generatedcats;
    print($gencats);

    var $pos = $gencats.lastIndexOf(">"); <<< this is line 6
    print($pos);

    if ($pos >=0)
    {
    $end = $gencats.length;
    $lastcat = $gencats.substring($pos, $end);
    }
    else $lastcat = $gencats;

    print($lastcat);

    return $lastcat;
    =============================

    I'm calling it just from a display option, and right now, only having it print to screen, so I can see that it works.

    Can anyone help with this? I admit, I'm very new at this javascript stuff. Are there certain functions that arent available to SM (and if so, why's it in the Mike Sanders book)?

    Any help would be greatly appreciated.

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    930

    Default

    What kind of field is "generatedcats"?

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    22

    Default

    Ooo... that's a good point. The field is an array (tho it's only one element). If I display the field in RAD Debugger, I get the following: {"Infrastructure SOPs > Data Center"}. So, it's an array, but just one element. Based on this, I tried updating line 6 to the following:

    var $gencats = record.generatedcats.toString()

    That at least got me past the error, but my output isnt quite what I was looking for:

    > Data Center Services"}
    43
    [C++ object Datum] - {"Infrastructure SOPs > Data Center Services"}

    I'm printing in three different places, just for troubleshooting purposes. The first line, above, is the important one. To get rid of the "> " I can just add 2 to my position variable, and I guess I can find something to remove the last two characters.

    Is there a better function to use then toString()? Would I be better to just pass the first element of the field (ie: 1 in generated cats in $L.file) instead of passing the whole record?

    Thanks for your help!

  4. #4
    Senior Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    930

    Default

    Passing the first line makes more sense indeed. Just do:
    var $gencats = record.generatedcats[0];

    that should work.

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    22

    Default

    Thank you for your help. This worked for me. I ran into an issue later where it would print my values to the message window, but in a later part of the script, where I tried to add that data to another field, it wouldnt work.

    ie: extract the last level of the generatedcats field, and add it to the subject line of the change control.

    In looking thru the Javascript Basics book, I discovered that javascript variables cant start with the $. So, while I guess it was ok for just outputing to the screen, once I actually tried to use that variable, it wouldnt work because of the $.

    To get past this, I just removed the $ and added letters to designate data that came from Service Manager (ie: $gencats became smgencats) vs a variable used within the script (ie: $pos became vpos).

    Anyway, thanks for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts