Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

setting object key dynamically in javascript

Suppose I have an object like this :

let inputData = {
  "dimensions": [
    {
      "id": "dimension_re",
      "label": "Region",
      "values": ["East", "East", "West", "SouthWest", "South","NorthEast"]
    }, 
    {
      "id": "dimension_cnt",
      "label": "County",
      "values": ["London", "Italy", "Germany", "US", "Russia","India"]
    },
    {
      "id": "measure_sales",
      "label": "Sales",
      "values": [100, 156, 432, 462, 25,100]
    }, 
    {
      "id": "measure_qty",
      "label": "Quantity",
      "values": [85, 34, 153, 434, 52, 43]
    }, 
    {
      "id": "measure_profit",
      "label": "Profit",
      "values": [123, 45, 421, 465, 451, 56]
    }
  ]
}

Now my expected output would be this:

let expectdData = [
  {
    "Region": "East",
    "County": "London",
    "Sales": 100,
    "Quantity": 85,
    "Profit": 123
  }, 
  {
    "Region": "East",
    "County": "Italy",
    "Sales": 156,
    "Quantity": 34,
    "Profit": 45
  }, 
  {
    "Region": "West",
    "County": "Germany",
    "Sales": 432,
    "Quantity": 153,
    "Profit": 421
  }, 
  {
    "Region": "SouthWest",
    "County": "US",
    "Sales": 462,
    "Quantity": 434,
    "Profit": 465
  }, 
  {
    "Region": "South",
    "County": "Russia",
    "Sales": 25,
    "Quantity": 52,
    "Profit": 451
  },
  {
    "Region": "NorthEast",
    "County": "India",
    "Sales": 100,
    "Quantity": 43,
    "Profit": 56
  }
]

So here is my Program to get this expected data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let actualData = [];

inputData.dimensions.forEach((e,i) => {
  let tempVal = e.label;
  e.values.forEach((elem,index) => {
    actualData[index] = new Object({
      [tempVal] : elem
    });
  })
});

console.log(actualData);

But unfortunately, I only get the last item for every object. In my console it looks like this:

[
  { Profit: 123 },
  { Profit: 45 },
  { Profit: 421 },
  { Profit: 465 },
  { Profit: 451 },
  { Profit: 56 }
]

I think, in every iteration, it just overrides the "tempVal" variable. How to prevent this & how can I achieve the expected array of objects? Kindly let me know.

>Solution :

You are replacing the whole object on each iteration, you just need to create it if it does not exists, otherwise you can replace the key.

let actualData = [];

inputData.dimensions.forEach((e,i)=>{
  let tempVal = e.label;
  e.values.forEach((elem,index) => {
    if (!actualData[index]) {
      actualData[index] = {}
    }
    actualData[index][tempVal] = elem
  })
});

console.log(actualData);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading