2 Replies Latest reply on Mar 12, 2007 9:48 AM by Newsgroup_User

    Array.join

    lergo
      I have an array that looks like this:
      var myArray:Array = [{name:"Peter", email:"peter@web.com"}, {name:"Mark", email:"mark@web.com"}];

      I would like to join the array to achieve this result:
      var namesJoined:String = "Peter|Mark";
      var emailsJoined:String = "peter@web.com|mark@web.com";

      As a start I have tried this
      trace(myArray.join("|"));
      which returns
      [object Object]|[object Object]

      Is there an easy way to do this without a foor loop? Can someone please help me? Thanks!

        • 1. Re: Array.join
          Level 7
          I don't understand your aversion to using a for loop.



          var myArray:Array = [{name:"Peter", email:"peter@web.com"}, {name:"Mark",
          email:"mark@web.com"}];


          function joinOnField(arr:Array, sep:String, fieldName:String){
          if(fieldName==undefined){return arr.join(sep);}
          var tempArray:Array = new Array()
          for(var i:Number = 0; i< arr.length; i++){
          tempArray.push(arr [fieldName]);
          }
          return tempArray.join(sep);
          }

          trace(joinOnField(myArray, "|", "name"));
          trace(joinOnField(myArray, "|", "email"));


          • 2. Re: Array.join
            kglad Adobe Community Professional & MVP
            that's not much of a start: if you trace your array you'll get the same thing separated by commas. and why no for-loop?