• This topic has 6 replies, 4 voices, and was last updated 8 years ago by bash.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Help with some simple Javascript
  • bash
    Free Member

    well obviously not simple to me but I’m sure it’ll be a piece of cake to some of you. I’m trying to concatenate some segments of an HL7 message but not really getting anywhere, this is the message:

    NTE|1|T| Hi|
    NTE|2|T| this|
    NTE|3|T| is|
    NTE|4|T| really|
    NTE|5|T| annoying|
    NTE|6|T| me|

    the code I have is this but not doing anything:

    var strFinalNTE = ”;

    for each (seg in msg.NTE)
    {
    strFinalNTE = strFinalNTE + seg [‘NTE.3’][‘NTE.3.1’].toString();
    }

    Any ideas?

    aroyalnit
    Free Member

    Should be pretty simple. What’s the format of the input message – a multiple-line string, or something else? An example of your expected result in strFinalNTE would be helpful also!

    GrahamA
    Free Member

    Hard (for me) to say without knowing a bit more about the HL7 message structure – I’m not sure if the structure you have sketched matches the structure you are iterating through

    Here are some examples of iterating through an object/data structure from Stack Overflow

    How to Loop through JavaScript object literal with objects as members?

    Iterate through object properties

    I think that the first may help you

    lemonysam
    Free Member

    is this in a broswer, what error’s it’s throwing? I assume you knowit should be “forEach”.

    lemonysam
    Free Member

    There’s quite a few things in there that aren’t right.

    Anyhow, assuming no preprocessing and the msg is just a multi-line string:

    var msg = “NTE|1|T| Hi|\n”
    +”NTE|2|T| this|\n”
    +”NTE|3|T| is|\n”
    +”NTE|4|T| really|\n”
    +”NTE|5|T| annoying|\n”
    +”NTE|6|T| me|”;

    var strFinalNTE = ”;

    var msgSplit = msg.split(“\n”);
    msgSplit.forEach(
    function append(value) {
    strFinalNTE += value.split(“|”)[3].toString();

    }
    )

    alert(strFinalNTE);

    https://jsfiddle.net/aa89ntpk/2/

    lemonysam
    Free Member

    Got bored whilst on phone to marketing man…

    If you’re doing this regularly then…

    String.prototype.HlCodeToArray = function (){
    var output = [];
    var msgSplit = this.split(“\n”);
    output = msgSplit.map(function(value) {
    return value.split(“|”);
    });
    return output;
    };

    var msg = “NTE|1|T| Hi|\n”
    +”NTE|2|T| this|\n”
    +”NTE|3|T| is|\n”
    +”NTE|4|T| really|\n”
    +”NTE|5|T| annoying|\n”
    +”NTE|6|T| me|”;

    var strFinalNTE = ”;

    var msgArray = msg.HlCodeToArray();
    msgArray.forEach(
    function(value) {
    strFinalNTE += value[3].toString();

    }
    );

    document.getElementById(‘output’).innerHTML = strFinalNTE;

    https://jsfiddle.net/aa89ntpk/21/

    bash
    Free Member

    Cheers guys managed to work it out plus delete the old ‘fields’ so it just comes through as the one message. Don’t do this a lot hence the daft question.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Help with some simple Javascript’ is closed to new replies.