Help with some simp...
 

MegaSack DRAW - 6pm Christmas Eve - LIVE on our YouTube Channel

[Closed] Help with some simple Javascript

6 Posts
4 Users
0 Reactions
61 Views
 bash
Posts: 0
Free Member
Topic starter
 

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?


 
Posted : 01/10/2015 7:39 am
Posts: 124
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!


 
Posted : 01/10/2015 7:51 am
Posts: 19
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

[url= http://stackoverflow.com/q/921789 ]How to Loop through JavaScript object literal with objects as members?[/url]

[url= http://stackoverflow.com/q/8312459 ]Iterate through object properties
[/url]

I think that the first may help you


 
Posted : 01/10/2015 7:54 am
Posts: 0
Free Member
 

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


 
Posted : 01/10/2015 7:57 am
Posts: 0
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/


 
Posted : 01/10/2015 8:47 am
Posts: 0
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/


 
Posted : 01/10/2015 11:05 am
 bash
Posts: 0
Free Member
Topic starter
 

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.


 
Posted : 01/10/2015 11:32 am