Map New array of object from array of object javascript

Parth Raval

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?

trincot

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:

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Map object with array in javascript

From Dev

Map array of an array to an object in JavaScript

From Dev

Create new Array of Object from Array of Object

From Dev

Map array object values to new array

From Dev

How to map an array object with key from different array set in javascript

From Dev

How to print a new array from existing array of object javascript

From Dev

Javascript Array Map by Object Attributes

From Dev

Map a javascript object into array structure

From Javascript

Trying to map an Array to an Object - Javascript

From Dev

map single object from JSON array to new JSON list.

From Dev

Formatting Javascript Object to new Array

From Dev

JavaScript object array new values

From Dev

return object from array map

From Dev

javascript object parsing and creating new object array

From Dev

Create new object from array

From Dev

output [object object] from array .map

From Dev

Create new object from existing array and object

From Dev

How to map values from an array to an array of Object?

From Dev

Map object values from an array within an array

From Dev

Get a new array from an array of object

From Dev

Copy values from an object array into a new array

From Dev

JavaScript map id from one array of object to another

From Dev

Filtering object from an array by checking a matched value in a map (JavaScript)

From Dev

how to map more than one property from array of object in javascript

From Dev

Cannot pass array object from map function as argument javascript

From Dev

ASPNET Map a property of an object into new array

From Dev

Javascript new array prototype in new object constructor

From Dev

How to reset broken JavaScript Array Object from new iframe?

From Dev

JavaScript - New object array from matched ID's

Related Related

  1. 1

    Map object with array in javascript

  2. 2

    Map array of an array to an object in JavaScript

  3. 3

    Create new Array of Object from Array of Object

  4. 4

    Map array object values to new array

  5. 5

    How to map an array object with key from different array set in javascript

  6. 6

    How to print a new array from existing array of object javascript

  7. 7

    Javascript Array Map by Object Attributes

  8. 8

    Map a javascript object into array structure

  9. 9

    Trying to map an Array to an Object - Javascript

  10. 10

    map single object from JSON array to new JSON list.

  11. 11

    Formatting Javascript Object to new Array

  12. 12

    JavaScript object array new values

  13. 13

    return object from array map

  14. 14

    javascript object parsing and creating new object array

  15. 15

    Create new object from array

  16. 16

    output [object object] from array .map

  17. 17

    Create new object from existing array and object

  18. 18

    How to map values from an array to an array of Object?

  19. 19

    Map object values from an array within an array

  20. 20

    Get a new array from an array of object

  21. 21

    Copy values from an object array into a new array

  22. 22

    JavaScript map id from one array of object to another

  23. 23

    Filtering object from an array by checking a matched value in a map (JavaScript)

  24. 24

    how to map more than one property from array of object in javascript

  25. 25

    Cannot pass array object from map function as argument javascript

  26. 26

    ASPNET Map a property of an object into new array

  27. 27

    Javascript new array prototype in new object constructor

  28. 28

    How to reset broken JavaScript Array Object from new iframe?

  29. 29

    JavaScript - New object array from matched ID's

HotTag

Archive