Thursday, June 20, 2013

Use join() in JavaScript to concatenate the items in an array to a string wherein the array items are separated by commas.

define([], function() {
   return {
      getTargetById: function(id, tabToBe){
         var pieces = id.split("_");
         pieces.shift();
         pieces.push(tabToBe);
         return pieces.join(',');
      }
   };
});

 
 

If we hand in "foo_bar_baz" for id and "qux" for tabToBe, we get back "bar,baz,qux" from our AMD module above. If the comma were left out of the join and the join just looks like join() then the comma would be used as a default.

No comments:

Post a Comment