title: In Javascript, when I separate the strings, how should I run the method for each string after the split?
body: <pre><code>function processPathLettersIntoArray(source){
      source = source.split(&quot;M&quot;).join(&quot;;M&quot;);
      source = source.split(&quot;L&quot;).join(&quot;;L&quot;);
      source = source.split(&quot;z&quot;).join(&quot;;z&quot;);
      source = source.split(&quot;;&quot;);
      source = source.slice(1);
      return source.map(mapGroup);
  }
  </code></pre>
  <p>This is my split method.</p>
  <pre><code>function gcodeXY(source){
     var [xVal, yVal] = source.split(&quot;,&quot;);
     return `X${xVal} Y${yVal}`
  }
  let start=&quot;X0 Y0&quot;; //another side effect :-(
  
  function mapGroup(grp){
      let s=grp.split(&quot; &quot;);
      if (s[0]==&quot;M&quot;){
          start=gcodeXY(s[1]);
          last=s[1];
          return &quot;G0 F200 &quot;+start;
      }
      else if (s[0]==&quot;L&quot;){
          start=gcodeXY(s[1]);
          last=s[1];
          return &quot;G1 F100 &quot;+start;
      }
      else if (s[0]==&quot;z&quot;){
          last=s[1];
          return &quot;G1 F100 &quot;+start;
      }
      return grp;
  }
  </code></pre>
  <p>This is how I want each string to be achieved after splitting.</p>
  <pre><code>function path2gcode(source){
      source = processPathLettersIntoArray(source);
      return source;
  }
  </code></pre>
  <p>This is the method I need to run.
  What I want ask is that in Javascript, when I separate the strings, how should I run the method for each string after the split?The picture shows the input and output examples and my current output.<a href="https://i.stack.imgur.com/oSqfQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oSqfQ.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/e7ZAE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e7ZAE.png" alt="enter image description here" /></a></p>