Camillia
asked on
Anyway to format this array data differently?
1. I have an array and the data looks like below
2. The data looks like this
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?
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);
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
have a look at this
https://jsfiddle.net/7ouymjam/
it returns 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));
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}]
}
ASKER
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]},
];
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.
ASKER
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
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);
ASKER
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'll post back.
ASKER
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]},
];
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Let me see, I've been at this for 4 hours so ugly is ok if it works :) I'll post back .
ASKER
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]}]
[{"name": "county1", "data": [144, 333]} , {"name": "county2", "data ": [123, 2302]}]
ASKER