String operation with an array of strings

I have an array of strings, like the example below

["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"]

u can see that all sentences in the array can be divided by . , and I need to make the logical template algo to make the sentence meaningful by taking the array in the reverse order and return the words at the end. if u read it last, because i.am.new.to.coding , taking the last spit value from each sentence, finally makes a meaningful sentence. I am trying to create such code in javascript or jquery and I am stuck with this for more than a day. since it is so difficult.

any script expert plz will help to do this. I appreciate your help. TIA

+5
source share
4 answers

Seems like a direct, reversible array, displays it, returning the last part after a period, then concatenates with spaces

 var arr = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"]; var s = arr.reverse().map(function(x) { return x.split('.').pop(); }).join(' '); document.body.innerHTML = s; 
+6
source

 var a = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"]; var s = a.reduceRight(function(x,y){ return x + '.' + y.split('.').pop(); }); document.body.textContent = s; 
+2
source

This worked for me:

 var array = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"] var b = []; for(i=array.length-1;i>=0;i--) { var a = array[i].split('.').pop() b += " "+a alert(a) } alert(b) 
+1
source

Another way:

 arr = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"]; arr = arr.reverse(); str = ''; for(i=0;i<arr.length;i++) { data = arr[i].split('.'); len = data.length; str = str + data[len-1] + " "; } console.log(str); 
+1
source

Source: https://habr.com/ru/post/1207365/


All Articles