繁体   English   中英

在Javascript中将数组的对象转换为数组的数组

[英]Converting an object of arrays into array of arrays in Javascript

object = {
          'AAA' : [1, 2, 3],
          'BBB' : [4, 5, 6]
      }

我有一个看起来像这样的对象,但是我需要将其转换成看起来像这样的数组:

arr = [['AAA', 'BBB'],
       [1, 4],
       [2, 5],
       [3, 6]]

为了能够传递给我正在使用的excel库。

转换像Javascript这样的格式的好方法是什么?

给定方案的解决方案:

 const objj = { 'AAA' : [1, 2, 3], 'BBB' : [4, 5, 6] } const getFormattedArray = (objJ) => { const formatted = [] const keys = Object.keys(objJ) const values = Object.values(objJ) const [val1, val2] = [...values] formatted.push(keys) getFormattedChild(val1, val2).forEach(item => { formatted.push(item) }) return formatted } const getFormattedChild = (a, b) => { let blob = [] for (let i = 0; i < a.length; i++){ let derp = [] derp.push(a[i]) derp.push(b[i]) blob.push(derp) } return blob } const result = getFormattedArray(objj) console.log('result: \\n', result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

解决方案,如果AAABBB具有不同的数组值:

 const objj = { 'AAA' : [1, 2, 3, null], 'BBB' : [4, 5, 6, 7, 8] } const getFormattedArray = (objJ) => { const formatted = [] const keys = Object.keys(objJ) const values = Object.values(objJ) const [val1, val2] = [...values] formatted.push(keys) getFormattedChild(val1, val2).forEach(item => { formatted.push(item) }) return formatted } const getFormattedChild = (a, b) => { let blob = [] const aLength = a.length const bLength = b.length const upperLimit = (aLength <= bLength) ? bLength : aLength for (let i = 0; i < upperLimit; i++){ let derp = [] derp.push(a[i]) derp.push(b[i]) blob.push(derp) } return blob } const result = getFormattedArray(objj) console.log('result: \\n', result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

您可以采用对象的键,然后对键的外部迭代和值的内部迭代采用嵌套方法。

然后分配一个新数组(如果未提供),并获取外部数组的索引以分配值。 以后用键作为第一个数组合并结果。

 var object = { AAA : [1, 2, 3], BBB : [4, 5, 6, 8] }, keys = Object.keys(object), array = [keys].concat(keys.reduce((r, k, j) => { object[k].forEach((v, i) => (r[i] = r[i] || [])[j] = v); return r; }, [])); console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

这里有一个方法来简洁地做到这一点在一条线的基础上,解决方案在这里

const arr = [Object.keys(object), Object.values(object)[0].map((col, i) => Object.values(object).map(row => row[i]))];

 var object = { 'AAA' : [1, 2, 3], 'BBB' : [4, 5, 6] }; const arr = [Object.keys(object), Object.values(object)[0].map((col, i) => Object.values(object).map(row => row[i]))]; console.log(arr); 

var obj = {
          'AAA' : [1, 2, 3],
          'BBB' : [4, 5, 6]
      };

const result = [Object.keys(obj)];
result.concat(obj[Object.keys(obj)[0]].reduce((acc, ele, i)=>{
  acc.push([ele,obj[Object.keys(obj)[1]][i]]);
  return acc;
},[]));

输出:

[ [ 'AAA', 'BBB' ], [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

暂无
暂无

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

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