SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array of objects

    Hello! I currently have an array named which contains objects as follows:

    Code JavaScript:
    var downloads=
    [{ name:"product1", value: 48, date: new Date("2013/11/01") },
    {name:"product1", value: 50, date: new Date("2013/11/02") },
    {name:"product2", value: 55, date: new Date("2013/11/03") },
    {name:"product2", value: 35, date: new Date("2013/11/04") }
    ];

    To appropriately plot these data on a graph (using KendoUI), I need to obtain an array as follows:
    Code JavaScript:
    var data=
    [
    {name:"product1", data: [{value: 48, date: new Date("2013/11/01") }, {value: 50, date: new Date("2013/11/02") }]},
    {name:"product2", data: [{value: 55, date: new Date("2013/11/03") }, {value: 35, date: new Date("2013/11/04") }]}
    ];

    How would I need to proceed?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Welcome to the forums

    Something like this should do:

    Code:
    var downloads=
      [{ name:"product1", value: 48, date: new Date("2013/11/01") },
      {name:"product1", value: 50, date: new Date("2013/11/02") },
      {name:"product2", value: 55, date: new Date("2013/11/03") },
      {name:"product2", value: 35, date: new Date("2013/11/04") }
    ],
    data = []
    
    for(i=0, len = downloads.length; i<len; i=i+2){
    
      var item1 = downloads[i],
          item2 = downloads[i+1],
          obj = {},
          objData = [];
    
      obj["name"] = item1["name"];
      delete item1["name"];
      delete item2["name"];
    
      objData.push(item1);
      objData.push(item2);
    
      obj["data"] = objData;
      data.push(obj);
    }
    
    console.log(data);
    Is this moving in the right direction?

  3. #3
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. The returned array seems ok. Will try passing it into Kendo UI.

    Btw,
    Code:
    Console.log()
    doesn't seem to work. I've had to use
    Code:
    alert(<array>.toSource());
    instead to be able to view the returned array.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Glad that seems ok

    Quote Originally Posted by girish3110 View Post
    Btw,
    Code:
    Console.log()
    doesn't seem to work.
    Could you elaborate on this.

    Apart form the fact that it is console with a small "c", what didn't work for you?
    What output was shown in the console?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •