📜  如果字符串的值大于 3,则反转字符串 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:36.556000             🧑  作者: Mango

代码示例1
var s = 'This is a short sentence' // set test sentence
  , e = s.split(' ')               // 'This is a short sentence' ==> ['This','is','a','short','sentence']
         .map(function(v,i,a){     // REPLACE the value of the current index in the array (run for each element in the array)
            return v.length > 4    // IF the length of the a 'word' in the array is greater than 4
                 ? v.split('')     // THEN return: 'word' ==> ['w','o','r','d']
                    .reverse()     // ['w','o','r','d'] ==> ['d','r','o','w']
                    .join('')      // ['d','r','o','w'] ==> 'drow'
                 : v;              // OR return: the original 'word'
         }).join(' ');             // ['This','is','a','trohs','ecnetnes'] ==> 'This is a trohs ecnetnes'

console.log(e); // 'This is a trohs ecnetnes'