title: Spliting values based on condition in JavaScript array
body: <p>Look at the below code or this <a href="http://jsfiddle.net/aAy6L/1/" rel="nofollow">fiddle</a></p>
  
  <pre><code>var arr = ["[Dim1].[Mem1].&amp;[1]",
             "[Dim2].[Mem1].&amp;[2]",
             "[Dim1].[Mem1].&amp;[5]",
             "[Dim2].[Mem1].&amp;[77]",
             "[Dim2].[Mem1].[All].Child",
             "[Dim3].[Mem1].&amp;[1]"];
  
  var res = _.chain(arr)
      .invoke("split", ".&amp;")
      .groupBy(0)
      .map(function (els) {
          return "{ " + _.invoke(els, "join", ".&amp;").join(", ") + " }";
      })
      .value();
  
  console.log(res);
  </code></pre>
  
  <p><strong>this produces following output:</strong></p>
  
  <pre><code>[
  "{ [Dim1].[Mem1].&amp;[1], [Dim1].[Mem1].&amp;[5] }", 
  "{ [Dim2].[Mem1].&amp;[2], [Dim2].[Mem1].&amp;[77] }", 
  "{ [Dim2].[Mem1].[All].Child }", 
  "{ [Dim3].[Mem1].&amp;[1] }"
  ]
  </code></pre>
  
  <p>The above code is using lodash library. It looks at the array and splits the values by <code>'.&amp;'</code> and then groups the data by the resultant array's zero'th index. </p>
  
  <p>This code is working fine and its doing whats its suppose to do.</p>
  
  <p>Now consider a scenario where the input array does not have <code>'.&amp;'</code> in it. In that case I want to split the input by <code>'.'</code> and then group the values. Is it possible to achieve the desired result by modifying code above? Or is it possible to do conditional split based on the input value?</p>
  
  <pre><code>var arrTwo = ["[Dim1].[Mem1].&amp;[1]",
                "[Dim2].[Mem1].&amp;[2]",
                "[Dim1].[Mem1].&amp;[5]",
                "[Dim2].[Mem1].&amp;[77]",
                "[Measures].[M11]",
                "[Dim2].[Mem1].[All].Child",
                "[Dim3].[Mem1].&amp;[1]"],
                "[Measures].[M1]",
                "[Measures].[M2]";
  </code></pre>
  
  <p><strong>Expected output:</strong></p>
  
  <pre><code>[
  
  "{ [Dim1].[Mem1].&amp;[1], [Dim1].[Mem1].&amp;[5] }" , 
  "{ [Dim2].[Mem1].&amp;[2], [Dim2].[Mem1].&amp;[77] }", 
  "{ [Dim2].[Mem1].[All].Child }", 
  "{ [Dim3].[Mem1].&amp;[1] }",
  "{[Measures].[M11], [Measures].[M1], [Measures].[M2]}"
  ]
  </code></pre>