I am trying to loop through an array. The array is basically a list from a .txt file on a server, which looks like this:
12345|Test message
55555|55555's message
12345|Test message 2
I do a simple GET request to the server, then get the response in a variable called res. I then do this:
array = res.split("\n");
for(i = 0; i < array.length; i++) {
array2 = array[i].split("|");
if(array2[0] == "12345") {
console.log(array[1]);
}
}
It will go through each line of the array, split it on | and then check if the first "element" is 12345. If it is, then write the message in the console. However, I want it to write the last message and not every message. In this case, it will write:
Test message
Test message 2
But I want it to write
Test message 2
The "question" is: Can you loop through the array, but only do something after it has found the last message? Thanks.