1

I have an object that is in this form:

{
    Health_id: [1, 2, 3],
    Epoch_Time: [16012,16013,16014],
    Disk_Free: ['86.2', '86.2','86.2'],
    Memory_Free: ['1555', '1552','1565'],
    CPU_Usage_Percent: ['6.7', '3.0', '2.7'],
    CPU_Time: ['24744.89','24749.60','24753.81']
}

I need to covert this into an array of objects like this:

[
    {'Health_id':1,'Epoch_Time':'16012','Disk_Free':'86.2','Memory_Free':'1555','CPU_Usage_Percent':'6.7','CPU_Time':'24744.89'}, 
    {'Health_id':2,'Epoch_Time':'16013','Disk_Free':'86.2','Memory_Free':'1552','CPU_Usage_Percent''3.0','CPU_Time':'24749.60'}, 
    {'Health_id':3,'Epoch_Time':'16014','Disk_Free':'86.2','Memory_Free':'1565','CPU_Usage_Percent': '2.7','CPU_Time':'24753.81'},
]

3 Answers 3

3

I am hoping that all the inner arrays have the same number of elements. You can try this way-

const data = {
  Health_id: [1, 2, 3],
  Epoch_Time: [16012,16013,16014],
  Disk_Free: ['86.2', '86.2','86.2'],
  Memory_Free: ['1555', '1552','1565'],
  CPU_Usage_Percent: ['6.7', '3.0', '2.7'],
  CPU_Time: ['24744.89','24749.60','24753.81']
};

const res = [];
const keys = Object.keys(data);

for(let i = 0, l = data.Health_id.length; i < l; i++) {
  const obj = {};
  keys.forEach(key => obj[key] = data[key][i]);
  res.push(obj);
}

console.log(res);
.as-console-wrapper{min-height: 100%!important; top: 0}

Sign up to request clarification or add additional context in comments.

Comments

1

Simple naive solution for your reference

const convert = (old) => {
  const nRecords = old.Health_id.length;
  const nData = [];
  for (let index = 0; index < nRecords; index++) {
    nData.push({
      Health_id: old.Health_id[index],
      Epoch_Time: old.Epoch_Time[index],
      Disk_Free: old.Disk_Free[index],
      Memory_Free: old.Memory_Free[index],
      CPU_Usage_Percent: old.CPU_Usage_Percent[index],
      CPU_Time: old.CPU_Time[index],
    });
  }

  return nData;
};

let data = {
  Health_id: [1, 2, 3],
  Epoch_Time: [16012, 16013, 16014],
  Disk_Free: ["86.2", "86.2", "86.2"],
  Memory_Free: ["1555", "1552", "1565"],
  CPU_Usage_Percent: ["6.7", "3.0", "2.7"],
  CPU_Time: ["24744.89", "24749.60", "24753.81"],
};

data = convert(data);
console.log(data);

Comments

1

With a simple for loop and the array.reduce() method you can just create the new objects.

const data = {
   Health_id: [1, 2, 3],
   Epoch_Time: [16012,16013,16014],
   Disk_Free: ['86.2', '86.2','86.2'],
   Memory_Free: ['1555', '1552','1565'],
   CPU_Usage_Percent: ['6.7', '3.0', '2.7'],
   CPU_Time: ['24744.89','24749.60','24753.81']
};

const size = Object.values(data)[0].length;
const keys = Object.keys(data);
const result = [];

for(let i = 0; i < size; i++) {
  const options = keys.reduce((acc, key) => {
    acc[key] = data[key][i];
    return acc;
  }, {})
  
  result.push(options);
}

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.