Angular js - Format string in modal format with correct spaces

I have a popup that has the following output! The output is just one complete line with spaces and newlines. But each line is concatenated with the previous line. Thus, each line can be configured individually.

Test1 : Success : 200 Test2 : Success : 200 Test3 : Success : 200 Test4 : Success : 200 Test5 : Success : 404 Test6 : Success : 401 

Since I have several such popups and several tests for each popup. Is there a way to format lines to have appropriate indentation? That is, I would like my conclusion to be:

 Test1 : Success : 200 Test2 : Success : 200 Test3 : Success : 200 Test4 : Success : 200 Test5 : Success : 404 Test6 : Success : 401 
+7
javascript angularjs
source share
1 answer

Here is what I will do:

First, split the string by \n to get each row in the array. Then split again with : and trim to remove the variable spaces.

Finally, attach them again, but with the first element added with additional space that will be the same for each of them.

 let str = "Test1 : Success : 200\nTest2 : Success : 200\nTest3 : Success : 200\nTest4 : Success : 200\nTest5 : Success : 404\nTest6 : Success : 401" let arr = str.split("\n") let res = arr.map(function(st) { let temp = st.split(":") return temp.map(s => s.trim()) }) let final = res.map(function(a) { a[0] = a[0] + " " return a.join(" : ") }) console.log(final) 
+5
source share

All Articles