简体   繁体   中英

How to convert an object with arrays as values to an array of objects

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'},
]

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}

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

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM