从JavaScript对象数组中映射新的对象数组。

huangapple go评论70阅读模式
英文:

Map New array of object from array of object javascript

问题

let newArr = arr.map(item => ({
  'CountryName': item['CountryName'].trim(),
  'SourceName': item['SourceName'].trim(),
  'data': Object.keys(item).filter(key => key.startsWith('Y_')).map(year => parseFloat(item[year]) || 0)
}));
console.log(newArr);
英文:

I have one array of object. I want to map new array.

I want to create this array into another array of object like this

let arr = [{
"rn": "1",
"EnergyParameterId": "4",
"CountryId": "1",
"CountryName": "Bangladesh ",
"EnergySourceId": "1",
"SourceName": "Nuclear",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "2",
"EnergyParameterId": "4",
"CountryId": "1",
"CountryName": "Bangladesh ",
"EnergySourceId": "2",
"SourceName": "Fossil fuels",
"UnitId": "",
"Y_2011": "176",
"Y_2012": "144",
"Y_2013": "218",
"Y_2014": "159",
"Y_2015": "149",
"Y_2016": "154.7000",
"CAGR": "-2.55"
},
{
"rn": "8",
"EnergyParameterId": "4",
"CountryId": "1",
"CountryName": "Bangladesh ",
"EnergySourceId": "8",
"SourceName": "Biomass and waste",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "9",
"EnergyParameterId": "4",
"CountryId": "1",
"CountryName": "Bangladesh ",
"EnergySourceId": "9",
"SourceName": "Hydroelectric pumped storage",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
}, {
"rn": "10",
"EnergyParameterId": "4",
"CountryId": "1",
"CountryName": "Bangladesh ",
"EnergySourceId": "11",
"SourceName": "Generation",
"UnitId": "",
"Y_2011": "771",
"Y_2012": "882",
"Y_2013": "1103",
"Y_2014": "1159",
"Y_2015": "1183",
"Y_2016": "1214.7000",
"CAGR": "9.52"
},
{
"rn": "11",
"EnergyParameterId": "4",
"CountryId": "5",
"CountryName": "Czech Republic ",
"EnergySourceId": "1",
"SourceName": "Nuclear",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "12",
"EnergyParameterId": "4",
"CountryId": "5",
"CountryName": "Czech Republic ",
"EnergySourceId": "2",
"SourceName": "Fossil fuels",
"UnitId": "",
"Y_2011": "1545",
"Y_2012": "2286",
"Y_2013": "3242",
"Y_2014": "4172",
"Y_2015": "4212",
"Y_2016": "4273.2400",
"CAGR": "22.56"
},
{
"rn": "13",
"EnergyParameterId": "4",
"CountryId": "5",
"CountryName": "Czech Republic ",
"EnergySourceId": "3",
"SourceName": "Hydroelectricity",
"UnitId": "",
"Y_2011": "3967",
"Y_2012": "3734",
"Y_2013": "4719",
"Y_2014": "4991",
"Y_2015": "5037",
"Y_2016": "5757.0000",
"CAGR": "7.73"
},
{
"rn": "22",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "2",
"SourceName": "Fossil fuels",
"UnitId": "",
"Y_2011": "769",
"Y_2012": "731",
"Y_2013": "742",
"Y_2014": "732",
"Y_2015": "746",
"Y_2016": "760.0000",
"CAGR": "-0.24"
},
{
"rn": "23",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "3",
"SourceName": "Hydroelectricity",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "24",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "4",
"SourceName": "Geothermal",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "25",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "5",
"SourceName": "Tide and wave",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "26",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "6",
"SourceName": "Solar",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "1",
"Y_2013": "2",
"Y_2014": "6",
"Y_2015": "10",
"Y_2016": "10.7120",
"CAGR": "0.00"
},
{
"rn": "27",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "7",
"SourceName": "Wind",
"UnitId": "",
"Y_2011": "113",
"Y_2012": "135",
"Y_2013": "151",
"Y_2014": "153",
"Y_2015": "165",
"Y_2016": "134.0000",
"CAGR": "3.47"
},
{
"rn": "28",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "8",
"SourceName": "Biomass and waste",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "4.8000",
"CAGR": "0.00"
},
{
"rn": "29",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "9",
"SourceName": "Hydroelectric pumped storage",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "30",
"EnergyParameterId": "4",
"CountryId": "8",
"CountryName": "Honduras ",
"EnergySourceId": "11",
"SourceName": "Generation",
"UnitId": "",
"Y_2011": "882",
"Y_2012": "867",
"Y_2013": "895",
"Y_2014": "891",
"Y_2015": "921",
"Y_2016": "909.5120",
"CAGR": "0.62"
},
{
"rn": "35",
"EnergyParameterId": "4",
"CountryId": "13",
"CountryName": "Sri Lanka ",
"EnergySourceId": "5",
"SourceName": "Tide and wave",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "36",
"EnergyParameterId": "4",
"CountryId": "13",
"CountryName": "Sri Lanka ",
"EnergySourceId": "6",
"SourceName": "Solar",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "8",
"Y_2015": "8",
"Y_2016": "8.2500",
"CAGR": "0.00"
},
{
"rn": "37",
"EnergyParameterId": "4",
"CountryId": "13",
"CountryName": "Sri Lanka ",
"EnergySourceId": "7",
"SourceName": "Wind",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "38",
"EnergyParameterId": "4",
"CountryId": "13",
"CountryName": "Sri Lanka ",
"EnergySourceId": "8",
"SourceName": "Biomass and waste",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "39",
"EnergyParameterId": "4",
"CountryId": "13",
"CountryName": "Sri Lanka ",
"EnergySourceId": "9",
"SourceName": "Hydroelectric pumped storage",
"UnitId": "",
"Y_2011": "0",
"Y_2012": "0",
"Y_2013": "0",
"Y_2014": "0",
"Y_2015": "0",
"Y_2016": "0.0000",
"CAGR": "0.00"
},
{
"rn": "40",
"EnergyParameterId": "4",
"CountryId": "13",
"CountryName": "Sri Lanka ",
"EnergySourceId": "11",
"SourceName": "Generation",
"UnitId": "",
"Y_2011": "22877",
"Y_2012": "23289",
"Y_2013": "24361",
"Y_2014": "25626",
"Y_2015": "26783",
"Y_2016": "26807.6500",
"CAGR": "3.22"
}
];
console.log(arr);

I want to convert this into

let newArr = [{
'CountryName': srilanka,
'SourceName': Nuclear,
'data': [0,0,0,0,0,0]
}];

I have tried to loop and map but it did not worked for me...

let createArrLineChart = [];
debugger;    
for (let index = 0; index < newArr ; index++) {
// const element = newArr [index];
// let createObj:any = {};
// createObj.Name = element.CountryName;
// createArrLineChart.push(createObj)
var result:any = newArr.map((person: any) => {
person.startsWith("Y_");
});
console.log(result)
console.log(newArr[index]);
}

How can I do this?

答案1

得分: 2

以下是您要翻译的内容:

  • 您尝试中的一些问题:
  • 循环条件 index < newArr 实际上应该是 index < newArr.length
  • 由于 person 是一个普通对象而不是字符串,调用 startsWith 不起作用。您需要迭代对象的键。
  • newArr 似乎是错误的引用,因为它将是从 arr 中获得的结果。

您可以通过解构来获取这两个名称,通过过滤器获取年份键。

例如:

const arr = [{...内容太长省略部分...}];

const newArr = arr.map(({CountryName, SourceName, ...rest}) => ({
    CountryName,
    SourceName,
    data: Object.keys(rest).filter(key => key.startsWith("Y_")).map(key => +rest[key])
}));

console.log(newArr);

请注意,上述代码是对您提供的代码的翻译和修改。如果您有其他问题或需要进一步的帮助,请告诉我。

英文:

Some of the issues in your attempt:

  • The loop condition index < newArr should actually be index < newArr.length
  • As person is a plain object and not a string, calling startsWith on it will not work. You need to iterate the object's keys.
  • newArr seems the wrong reference, as that would be the result from arr.

You could get the two names via destructuring, and the year-keys via a filter.

For example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [{&quot;rn&quot;: &quot;1&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;1&quot;,&quot;CountryName&quot;: &quot;Bangladesh &quot;,&quot;EnergySourceId&quot;: &quot;1&quot;,&quot;SourceName&quot;: &quot;Nuclear&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;2&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;1&quot;,&quot;CountryName&quot;: &quot;Bangladesh &quot;,&quot;EnergySourceId&quot;: &quot;2&quot;,&quot;SourceName&quot;: &quot;Fossil fuels&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;176&quot;,&quot;Y_2012&quot;: &quot;144&quot;,&quot;Y_2013&quot;: &quot;218&quot;,&quot;Y_2014&quot;: &quot;159&quot;,&quot;Y_2015&quot;: &quot;149&quot;,&quot;Y_2016&quot;: &quot;154.7000&quot;,&quot;CAGR&quot;: &quot;-2.55&quot;},{&quot;rn&quot;: &quot;8&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;1&quot;,&quot;CountryName&quot;: &quot;Bangladesh &quot;,&quot;EnergySourceId&quot;: &quot;8&quot;,&quot;SourceName&quot;: &quot;Biomass and waste&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;9&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;1&quot;,&quot;CountryName&quot;: &quot;Bangladesh &quot;,&quot;EnergySourceId&quot;: &quot;9&quot;,&quot;SourceName&quot;: &quot;Hydroelectric pumped storage&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;}, {&quot;rn&quot;: &quot;10&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;1&quot;,&quot;CountryName&quot;: &quot;Bangladesh &quot;,&quot;EnergySourceId&quot;: &quot;11&quot;,&quot;SourceName&quot;: &quot;Generation&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;771&quot;,&quot;Y_2012&quot;: &quot;882&quot;,&quot;Y_2013&quot;: &quot;1103&quot;,&quot;Y_2014&quot;: &quot;1159&quot;,&quot;Y_2015&quot;: &quot;1183&quot;,&quot;Y_2016&quot;: &quot;1214.7000&quot;,&quot;CAGR&quot;: &quot;9.52&quot;},{&quot;rn&quot;: &quot;11&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;5&quot;,&quot;CountryName&quot;: &quot;Czech Republic &quot;,&quot;EnergySourceId&quot;: &quot;1&quot;,&quot;SourceName&quot;: &quot;Nuclear&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;12&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;5&quot;,&quot;CountryName&quot;: &quot;Czech Republic &quot;,&quot;EnergySourceId&quot;: &quot;2&quot;,&quot;SourceName&quot;: &quot;Fossil fuels&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;1545&quot;,&quot;Y_2012&quot;: &quot;2286&quot;,&quot;Y_2013&quot;: &quot;3242&quot;,&quot;Y_2014&quot;: &quot;4172&quot;,&quot;Y_2015&quot;: &quot;4212&quot;,&quot;Y_2016&quot;: &quot;4273.2400&quot;,&quot;CAGR&quot;: &quot;22.56&quot;},{&quot;rn&quot;: &quot;13&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;5&quot;,&quot;CountryName&quot;: &quot;Czech Republic &quot;,&quot;EnergySourceId&quot;: &quot;3&quot;,&quot;SourceName&quot;: &quot;Hydroelectricity&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;3967&quot;,&quot;Y_2012&quot;: &quot;3734&quot;,&quot;Y_2013&quot;: &quot;4719&quot;,&quot;Y_2014&quot;: &quot;4991&quot;,&quot;Y_2015&quot;: &quot;5037&quot;,&quot;Y_2016&quot;: &quot;5757.0000&quot;,&quot;CAGR&quot;: &quot;7.73&quot;},{&quot;rn&quot;: &quot;22&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;2&quot;,&quot;SourceName&quot;: &quot;Fossil fuels&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;769&quot;,&quot;Y_2012&quot;: &quot;731&quot;,&quot;Y_2013&quot;: &quot;742&quot;,&quot;Y_2014&quot;: &quot;732&quot;,&quot;Y_2015&quot;: &quot;746&quot;,&quot;Y_2016&quot;: &quot;760.0000&quot;,&quot;CAGR&quot;: &quot;-0.24&quot;},{&quot;rn&quot;: &quot;23&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;3&quot;,&quot;SourceName&quot;: &quot;Hydroelectricity&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;24&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;4&quot;,&quot;SourceName&quot;: &quot;Geothermal&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;25&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;5&quot;,&quot;SourceName&quot;: &quot;Tide and wave&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},
{&quot;rn&quot;: &quot;26&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;6&quot;,&quot;SourceName&quot;: &quot;Solar&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;1&quot;,&quot;Y_2013&quot;: &quot;2&quot;,&quot;Y_2014&quot;: &quot;6&quot;,&quot;Y_2015&quot;: &quot;10&quot;,&quot;Y_2016&quot;: &quot;10.7120&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;27&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;7&quot;,&quot;SourceName&quot;: &quot;Wind&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;113&quot;,&quot;Y_2012&quot;: &quot;135&quot;,&quot;Y_2013&quot;: &quot;151&quot;,&quot;Y_2014&quot;: &quot;153&quot;,&quot;Y_2015&quot;: &quot;165&quot;,&quot;Y_2016&quot;: &quot;134.0000&quot;,&quot;CAGR&quot;: &quot;3.47&quot;},{&quot;rn&quot;: &quot;28&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;8&quot;,&quot;SourceName&quot;: &quot;Biomass and waste&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;4.8000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;29&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;9&quot;,&quot;SourceName&quot;: &quot;Hydroelectric pumped storage&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;30&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;8&quot;,&quot;CountryName&quot;: &quot;Honduras &quot;,&quot;EnergySourceId&quot;: &quot;11&quot;,&quot;SourceName&quot;: &quot;Generation&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;882&quot;,&quot;Y_2012&quot;: &quot;867&quot;,&quot;Y_2013&quot;: &quot;895&quot;,&quot;Y_2014&quot;: &quot;891&quot;,&quot;Y_2015&quot;: &quot;921&quot;,&quot;Y_2016&quot;: &quot;909.5120&quot;,&quot;CAGR&quot;: &quot;0.62&quot;},{&quot;rn&quot;: &quot;35&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;13&quot;,&quot;CountryName&quot;: &quot;Sri Lanka &quot;,&quot;EnergySourceId&quot;: &quot;5&quot;,&quot;SourceName&quot;: &quot;Tide and wave&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;36&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;13&quot;,&quot;CountryName&quot;: &quot;Sri Lanka &quot;,&quot;EnergySourceId&quot;: &quot;6&quot;,&quot;SourceName&quot;: &quot;Solar&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;8&quot;,&quot;Y_2015&quot;: &quot;8&quot;,&quot;Y_2016&quot;: &quot;8.2500&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;37&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;13&quot;,&quot;CountryName&quot;: &quot;Sri Lanka &quot;,&quot;EnergySourceId&quot;: &quot;7&quot;,&quot;SourceName&quot;: &quot;Wind&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;38&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;13&quot;,&quot;CountryName&quot;: &quot;Sri Lanka &quot;,&quot;EnergySourceId&quot;: &quot;8&quot;,&quot;SourceName&quot;: &quot;Biomass and waste&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;39&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;13&quot;,&quot;CountryName&quot;: &quot;Sri Lanka &quot;,&quot;EnergySourceId&quot;: &quot;9&quot;,&quot;SourceName&quot;: &quot;Hydroelectric pumped storage&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;0&quot;,&quot;Y_2012&quot;: &quot;0&quot;,&quot;Y_2013&quot;: &quot;0&quot;,&quot;Y_2014&quot;: &quot;0&quot;,&quot;Y_2015&quot;: &quot;0&quot;,&quot;Y_2016&quot;: &quot;0.0000&quot;,&quot;CAGR&quot;: &quot;0.00&quot;},{&quot;rn&quot;: &quot;40&quot;,&quot;EnergyParameterId&quot;: &quot;4&quot;,&quot;CountryId&quot;: &quot;13&quot;,&quot;CountryName&quot;: &quot;Sri Lanka &quot;,&quot;EnergySourceId&quot;: &quot;11&quot;,&quot;SourceName&quot;: &quot;Generation&quot;,&quot;UnitId&quot;: &quot;&quot;,&quot;Y_2011&quot;: &quot;22877&quot;,&quot;Y_2012&quot;: &quot;23289&quot;,&quot;Y_2013&quot;: &quot;24361&quot;,&quot;Y_2014&quot;: &quot;25626&quot;,&quot;Y_2015&quot;: &quot;26783&quot;,&quot;Y_2016&quot;: &quot;26807.6500&quot;,&quot;CAGR&quot;: &quot;3.22&quot;}];
const newArr = arr.map(({CountryName, SourceName, ...rest}) =&gt; ({
CountryName,
SourceName,
data: Object.keys(rest).filter(key =&gt; key.startsWith(&quot;Y_&quot;)).map(key =&gt; +rest[key])
}));
console.log(newArr);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 16:55:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定