Array of objects

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

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:

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?

Hi there,

Welcome to the forums :slight_smile:

Something like this should do:

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?

Thanks. The returned array seems ok. Will try passing it into Kendo UI.

Btw,

Console.log()

doesn’t seem to work. I’ve had to use

alert(<array>.toSource());

instead to be able to view the returned array.

Hi,

Glad that seems ok :slight_smile:

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?