Help - what am I doing wrong

I have a Satellite 2 line Kepler list, which is in a strict format and includes white spaces, which cannot be removed as they form the correct sequence for reading.
However the satellite name can have the white spaces removed.

I have my list for each sat like this

"SAUDISAT 1C (SO-50)
1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994
2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316"

Which I have now formatted to this

array[3]
0: object
col1: "SAUDISAT 1C (SO-50) whitespaces here to remove "
1: object
col1: "1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994"
2: object
col1: "2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316"

Note: This editor removes the multiple white spaces everywhere

I want to remove the spaces from the end of the Satellite name as it is easier to select in a switch node so I did this, which looks reasonable to me, but the output I get is triplicated, even though it does what I want.

function trim(sat) {
    let trimsat = sat.trim();
    msg.payload.sat = trimsat;
}

for (var i = 0; i < (msg.payload).length; i++) {   
    
    var sat = msg.payload[0].col1;
    trim(sat);

    var newMsg = {};
    
    newMsg.payload = {
        sat: msg.payload.sat,
        line1: msg.payload[1].col1,
        line2: msg.payload[2].col1,
    }
    node.send(newMsg);
}
return null;

result 3 times
sat: "SAUDISAT 1C (SO-50)"
line1: "1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994"
line2: "2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316"

How do I get this to work but just once per entry

There is a javascript function trimEnd() which should remove only trailing spaces.

Split the payload at new lines, trimEnd() for each line and rejoin.
image

AH I have done it.
It sees the msg.payload length as 3 times, I don't really understand it, but that fixes it

for (var i = 0; i < (msg.payload).length / 3; i++) {   

Yes I di that first off, but the only way I could get to figure splitting the lines up was to use a csv node, which put each line as col1: data col1: data col1: data that removed the whitespaces from everything including the two data lines, maybe it would work with your suggestion as trim end. I will try that and see.

Thanks but couldn't get that to work properly, I will stick with my own solution.
I can't really see why it thinks that each item is a separate message, which it must be doing to triplicate it. anyway I will not worry about it too much.

That's fine, whatever works for you.

I am intrigued though, can you test this flow?

[{"id":"7b2da7bc89bb9f96","type":"inject","z":"36b5ac52249af0ef","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":105,"y":240,"wires":[["2538166138bcfc71"]]},{"id":"2538166138bcfc71","type":"template","z":"36b5ac52249af0ef","name":"","field":"payload","fieldType":"msg","format":"text","syntax":"mustache","template":"SAUDISAT 1C (SO-50)                                       \n1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994\n2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316","output":"str","x":255,"y":240,"wires":[["04a1badac9c37316","06f1737a75e860cd"]]},{"id":"04a1badac9c37316","type":"split","z":"36b5ac52249af0ef","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":160,"y":300,"wires":[["f165be8f695c2292"]]},{"id":"f165be8f695c2292","type":"function","z":"36b5ac52249af0ef","name":"function 2","func":"msg.payload = msg.payload.trimEnd()\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":300,"wires":[["98abe04290a78857"]]},{"id":"98abe04290a78857","type":"join","z":"36b5ac52249af0ef","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":445,"y":300,"wires":[["0cc7a29c3f936b78"]]},{"id":"0cc7a29c3f936b78","type":"debug","z":"36b5ac52249af0ef","name":"After","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":575,"y":300,"wires":[]},{"id":"06f1737a75e860cd","type":"debug","z":"36b5ac52249af0ef","name":"Before","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":240,"wires":[]}]

For me, Before shows a string of 190 characters, After shows 151 - 39 trailing spaces removed from row 1

Yes that works fine, it's me.
It all goes horribly wrong because I split the the whole thing initially with a csv node.
I did a lot of silly code to get somewhere that was not actually needed.
Lack of experience and knowledge on my part and there is a much simpler way of doing the whole thing now I look at it again. I thought through this in stages instead of looking at the end game.
Thanks for your help. I shall look for the wood not the trees next time.