Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Anyway to format this array data differently?

1. I have an array and the data looks like below

 var seriesTest  =[];
 var ret = <I get the data from database with C# code>;

 for (var = i=0; i < ret.length; i++)
{
    seriesTest.push({ "name":ret[i].countyName, "rank": ret[i].rank, "year":ret[i].year  });
}

console.log(seriesTest);

Open in new window


2. The data looks like this
 0: Object
      name: "county1"
      rank: 333
      year: 2015

 1: Object
       name: "county2"
      rank: 2302
      year: 2015

 2: Object
       name: "county1"
      rank: 144
      year: 2014

 3: Object
       name: "county2"
      rank: 123
      year: 2014

Open in new window


3. You see the there are 2014 and 2015 years. There are two "county" names...county1 and county2. You see the "rank" as well
For the "data", I want the earliest year (2014) and then 2015
[
{"name": "county1", "data": [144,333]},
{"name": "county2", "data": [123,2302]},
];


Anyway to do this in Javascript?
SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

Let me try now. Thanks
have a look at this

https://jsfiddle.net/7ouymjam/

var data = [{
  name: "county1",
  rank: 333,
  year: 2015
}, {
  name: "county2",
  rank: 2302,
  year: 2015
}, {
  name: "county1",
  rank: 144,
  year: 2014
}, {
  name: "county2",
  rank: 123,
  year: 2014
}];

//{"name": "county1", "data": [144,333]},
//{"name": "county2", "data": [123,2302]}
var newdata = _.chain(data).sortBy("rank").sortBy("name").groupBy("name").value()
  //alert(JSON.stringify(newdata));
console.log(JSON.stringify(newdata));

Open in new window


it returns this

{
"county1":[{"name":"county1","rank":144,"year":2014},{"name":"county1","rank":333,"year":2015}],
"county2":[{"name":"county2","rank":123,"year":2014},{"name":"county2","rank":2302,"year":2015}]
}

Open in new window

The first one by Leo doesn't work. I just get the same array back.

The one by Huseyin is not correct. It has to be in this format

[
{"name": "county1", "data": [144,333]},
{"name": "county2", "data": [123,2302]},
];
As I tested in liveweave it works.
Please look at my first post. My data looks like this when I look at it in the console. Your data format is different.

To use yours, how can  I change my data to match your data format?

0: Object
      name: "county1"
      rank: 333
      year: 2015

 1: Object
       name: "county2"
      rank: 2302
      year: 2015

 2: Object
       name: "county1"
      rank: 144
      year: 2014

 3: Object
       name: "county2"
      rank: 123
      year: 2014
Something like this?
var data_2={
  0:{
      name: "county1",
      rank: 333,
      year: 2015
  },
  1:{
      name: "county2",
      rank: 2302,
      year: 2015 
  },
  2:{
      name: "county1",
      rank: 144,
      year: 2014 
  },
  3:{
      name: "county2",
      rank: 123,
      year: 2014
  }
  
};

var newData = [];

var objLenght=Object.keys(data_2).length;
for(var i=0;i<objLenght;i++){
  newData.push(data_2[i]);
}
newData.sort(function(a,b){
  return  a.year-b.year;
});

console.log(newData);

Open in new window

Changed the data I get back from C# code from array to JSON format. I'll try your solution again, Leo.

I'll post back.
I copied the same exact example in ID: 42168644
and I get an array of 4 objects.  That won't work. I copied the same exact code.

I don't get this

[
{"name": "county1", "data": [144,333]},
{"name": "county2", "data": [123,2302]},
];
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me see, I've been at this for 4 hours so ugly is ok  if it works :)  I'll post back .
Yeah, I need to make a change to your code to add a comma and [] but I think this is it!

[{"name": "county1", "data": [144, 333]} , {"name": "county2", "data ": [123, 2302]}]