From the object 1, I am trying to form another object 2. I have written this below piece of code but I dono is this the right approach to achieve object 2.
var arr = [];
object1.filter(function(value) {
return value.hasOwnProperty("This Year"); // Get only elements, which have such a key
}).map(function(value) {
arr.push({value:value["This Year"]}); // Extract the values only
});
Object 1
var obj=[
{
"sno": 1,
"Last Year": 10000
},
{
"sno": 2,
"Last Year": 11500
},
{
"sno": 3,
"Last Year": 12500
},
{
"sno": 4,
"This Year": 25400
},
{
"sno": 5,
"Last Year": 15000
},
{
"sno": 6,
"This Year": 29800
}
]
Object 2 - Desired Result
var dataset = [
{
"seriesname": "Last Year",
"data": [
{
"value": "10000"
},
{
"value": "11500"
},
{
"value": "12500"
},
{
"value": "15000"
}
]
},
{
"seriesname": "This Year",
"data": [
{
"value": "25400"
},
{
"value": "29800"
}
]
}
]
If anyone can help me with better approach. In my code I am hard coding the key name of object 1. Is it possible in any other way?