简体   繁体   English

将对象数组转换为2D数组

[英]Transform Array of objects into 2D array

I want transform an array of objects into 2D array (array of arrays). 我想将对象数组转换为2D数组(数组数组)。 I try with map and Object.keys(), but I want push the first value of each objects into the first array, the second value of each objects into the second array, etc ... Thank you for your answers. 我尝试使用map和Object.keys(),但我想将每个对象的第一个值压入第一个数组,将每个对象的第二个值压入第二个数组,以此类推...谢谢您的回答。

The good result 好的结果

[
  ['2', '5', '8', '10', '12'],
  ['4', '10', '16', '20', '24'],
  ['6', '15', '24', '30', '36'],
  ['10', '25', '40', '50', '60']
]

The actual result 实际结果

[
  ['2', '4', '6', '10'],
  ['5', '10', '15', '25'],
  ['8', '16', '24', '40'],
  ['10', '20', '30', '50'],
  ['12', '24', '36', '60']
]

 let obj = [ { line1: '2', line2: '4', line3: '6', line4: '10' }, { line1: '5', line2: '10', line3: '15', line4: '25' }, { line1: '8', line2: '16', line3: '24', line4: '40' }, { line1: '10', line2: '20', line3: '30', line4: '50' }, { line1: '12', line2: '24', line3: '36', line4: '60' } ]; var output = obj.map(function(obj) { return Object.keys(obj).map(function(key) { return obj[key]; }); }); console.log(output); 

You need the indices for transfoming and take the reversed indices for accessing the result set. 您需要索引进行转换,并取相反的索引来访问结果集。

 var array = [{ line1: '2', line2: '4', line3: '6', line4: '10' }, { line1: '5', line2: '10', line3: '15', line4: '25' }, { line1: '8', line2: '16', line3: '24', line4: '40' }, { line1: '10', line2: '20', line3: '30', line4: '50' }, { line1: '12', line2: '24', line3: '36', line4: '60' }], result = array.reduce((r, o, i) => { Object.keys(o).forEach((k, j) => (r[j] = r[j] || [])[i] = o[k]); return r; }, []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

For a stable result, you could take the keys as array in advance. 为了获得稳定的结果,您可以预先将键作为数组。

 var array = [{ line1: '2', line2: '4', line3: '6', line4: '10' }, { line1: '5', line2: '10', line3: '15', line4: '25' }, { line1: '8', line2: '16', line3: '24', line4: '40' }, { line1: '10', line2: '20', line3: '30', line4: '50' }, { line1: '12', line2: '24', line3: '36', line4: '60' }], keys = ['line1', 'line2', 'line3', 'line4'], result = array.reduce((r, o, i) => { keys.forEach((k, j) => (r[j] = r[j] || [])[i] = o[k]); return r; }, []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can also use following approach. 您也可以使用以下方法。

 let obj = [ { line1: '2', line2: '4', line3: '6', line4: '10' }, { line1: '5', line2: '10', line3: '15', line4: '25' }, { line1: '8', line2: '16', line3: '24', line4: '40' }, { line1: '10', line2: '20', line3: '30', line4: '50' }, { line1: '12', line2: '24', line3: '36', line4: '60' } ]; var output = []; for(var i = 0; i < obj.length; i++){ var index = 0; for(var values in obj[i]){ if(output[index] == undefined){ output[index] = []; } output[index].push(obj[i][values]); index++; } } console.log(output); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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