merging arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bush will disarm all workers next

    merging arrays

    I'm new to JavaScript. So please excuse my mistakes.
    I need help on merging two arrays into a third one.
    There is an array a = {188, 180, 159, 67 }
    There is another array b={'Rio de Janeiro', 'Sao Paulo', 'Brasilia',
    'Belo Horizonte'}
    How can I merge them to produce an array that is equivalent to this:
    bg.xValues[0] = [188,'Rio de Janeiro'];
    bg.xValues[1] = [180,'Sao Paulo'];
    bg.xValues[2] = [159,'Brasilia'];
    bg.xValues[3] = [67 ,'Belo Horizonte'];



    Thanks
    Dakshin
  • Michael Winter

    #2
    Re: merging arrays

    On 17 Apr 2004 05:49:56 -0700, Bush will disarm all workers next
    <dakshing64@yah oo.com> wrote:
    [color=blue]
    > I'm new to JavaScript. So please excuse my mistakes.
    > I need help on merging two arrays into a third one.
    > There is an array a = {188, 180, 159, 67 }
    > There is another array b={'Rio de Janeiro', 'Sao Paulo', 'Brasilia',
    > 'Belo Horizonte'}[/color]

    I assume you meant

    a = [ 188, 180, 159, 67 ];
    b = [ 'Rio de Janeiro', 'Sao Paulo', 'Brasilia', 'Belo Horizonte' ];

    Braces ({}) denote object literals, brackets ([]) denote array literals.
    [color=blue]
    > How can I merge them to produce an array that is equivalent to this:
    > bg.xValues[0] = [188,'Rio de Janeiro'];
    > bg.xValues[1] = [180,'Sao Paulo'];
    > bg.xValues[2] = [159,'Brasilia'];
    > bg.xValues[3] = [67 ,'Belo Horizonte'];[/color]

    function mergeArrays( x, y ) {
    var t = [], n = Math.min( x.length, y.length );

    for( var i = 0; i < n; ++i ) {
    t[ i ] = [ x[ i ], y[ i ]];
    }
    return t;
    }

    bg.xValues = mergeArrays( a, b );

    If you can guarantee that "a" and "b" will always have the same length,
    you can reduce the assignment to "n" to

    n = x.length; // or y.length

    Hope that helps,
    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Bush will disarm all workers next

      #3
      Re: merging arrays

      Michael Winter <M.Winter@bluey onder.co.invali d> wrote in message news:<opr6lqubv z5vklcq@news-text.blueyonder .co.uk>...[color=blue]
      > On 17 Apr 2004 05:49:56 -0700, Bush will disarm all workers next
      > <dakshing64@yah oo.com> wrote:
      >[color=green]
      > > I'm new to JavaScript. So please excuse my mistakes.
      > > I need help on merging two arrays into a third one.
      > > There is an array a = {188, 180, 159, 67 }
      > > There is another array b={'Rio de Janeiro', 'Sao Paulo', 'Brasilia',
      > > 'Belo Horizonte'}[/color]
      >
      > I assume you meant
      >
      > a = [ 188, 180, 159, 67 ];
      > b = [ 'Rio de Janeiro', 'Sao Paulo', 'Brasilia', 'Belo Horizonte' ];
      >
      > Braces ({}) denote object literals, brackets ([]) denote array literals.
      >[color=green]
      > > How can I merge them to produce an array that is equivalent to this:
      > > bg.xValues[0] = [188,'Rio de Janeiro'];
      > > bg.xValues[1] = [180,'Sao Paulo'];
      > > bg.xValues[2] = [159,'Brasilia'];
      > > bg.xValues[3] = [67 ,'Belo Horizonte'];[/color]
      >
      > function mergeArrays( x, y ) {
      > var t = [], n = Math.min( x.length, y.length );
      >
      > for( var i = 0; i < n; ++i ) {
      > t[ i ] = [ x[ i ], y[ i ]];
      > }
      > return t;
      > }
      >
      > bg.xValues = mergeArrays( a, b );
      >
      > If you can guarantee that "a" and "b" will always have the same length,
      > you can reduce the assignment to "n" to
      >
      > n = x.length; // or y.length
      >
      > Hope that helps,
      > Mike[/color]


      Thanks for the reply. I tried the code you've posted without success I used
      for(i=0; i < a.length; i++)
      bg.xValues[i]=[a[i], b[i]];

      and also
      for(i=0; i < a.length; i++)
      bg.xValues[i]=[eval(a[i]), eval(b[i])];

      The bg object is a strange beast that expects atomic values like 145 or 'test'.
      Any thoughts?

      Dakshin

      Comment

      • Michael Winter

        #4
        Re: merging arrays

        On 17 Apr 2004 13:58:30 -0700, Bush will disarm all workers next
        <dakshing64@yah oo.com> wrote:

        [snip]
        [color=blue]
        > Thanks for the reply. I tried the code you've posted without success I
        > used
        > for(i=0; i < a.length; i++)
        > bg.xValues[i]=[a[i], b[i]];
        >
        > and also
        > for(i=0; i < a.length; i++)
        > bg.xValues[i]=[eval(a[i]), eval(b[i])];[/color]

        Using eval() certainly won't do any good (there are very few reasons to
        *ever* use eval()).
        [color=blue]
        > The bg object is a strange beast that expects atomic values like 145 or
        > 'test'.
        > Any thoughts?[/color]

        Unless you can point to some documentation that explains what the bg
        object is, I haven't got a clue.

        Sorry,
        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Bush will disarm all workers next

          #5
          Re: merging arrays

          Michael Winter <M.Winter@bluey onder.co.invali d> wrote in message news:<opr6mdurs x5vklcq@news-text.blueyonder .co.uk>...[color=blue]
          > On 17 Apr 2004 13:58:30 -0700, Bush will disarm all workers next
          > <dakshing64@yah oo.com> wrote:
          >
          > [snip]
          >[color=green]
          > > Thanks for the reply. I tried the code you've posted without success I
          > > used
          > > for(i=0; i < a.length; i++)
          > > bg.xValues[i]=[a[i], b[i]];
          > >
          > > and also
          > > for(i=0; i < a.length; i++)
          > > bg.xValues[i]=[eval(a[i]), eval(b[i])];[/color]
          >
          > Using eval() certainly won't do any good (there are very few reasons to
          > *ever* use eval()).
          >[color=green]
          > > The bg object is a strange beast that expects atomic values like 145 or
          > > 'test'.
          > > Any thoughts?[/color]
          >
          > Unless you can point to some documentation that explains what the bg
          > object is, I haven't got a clue.
          >
          > Sorry,
          > Mike[/color]

          Here is the link that has Javascript that uses bg at the bottom of the
          page. I've the Graph.js downloaded on another machine. I'll post the
          details soon.

          Comment

          Working...