英文:
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 < newArrshould actually beindex < newArr.length - As
personis a plain object and not a string, callingstartsWithon it will not work. You need to iterate the object's keys. newArrseems the wrong reference, as that would be the result fromarr.
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 = [{"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"}];
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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论