简体   繁体   English

JavaScript 中操作数组和对象的数据

[英]Data of Manipulating Arrays and Objects in JavaScript

I'm looking to perform some data manipulation of an object/array by somewhat transposing the values.我希望通过对值进行一些转置来对对象/数组进行一些数据操作。 where the values of the rows become columns.其中行的值成为列。 As an example i have original data which looks like this例如,我有看起来像这样的原始数据

 [ { "Team": "D PG", "Original_x0020_Size_x0020__x0028": 194, "Month": "January" }, { "Team": "D PP", "Original_x0020_Size_x0020__x0028": 143, "Month": "January" }, { "Team": "D RE", "Original_x0020_Size_x0020__x0028": 19, "Month": "January" }, { "Team": "D GP", "Original_x0020_Size_x0020__x0028": 3, "Month": "January" }, { "Team": "D PC", "Original_x0020_Size_x0020__x0028": 2, "Month": "January" }, { "Team": "D PT", "Original_x0020_Size_x0020__x0028": 2, "Month": "January" }, { "Team": "D PG", "Original_x0020_Size_x0020__x0028": 35, "Month": "February" }, { "Team": "D PP", "Original_x0020_Size_x0020__x0028": 120, "Month": "February" }, { "Team": "D RE", "Original_x0020_Size_x0020__x0028": 40, "Month": "February" }, { "Team": "D GP", "Original_x0020_Size_x0020__x0028": 30, "Month": "February" }, { "Team": "D PC", "Original_x0020_Size_x0020__x0028": 9, "Month": "February" }, { "Team": "D PT", "Original_x0020_Size_x0020__x0028": 6, "Month": "February" } ]

My intention to change this to我打算将其更改为

 //i'm looking at changing the original data to look like this. The Team will have a position on the array, for example array[0][1](this column will be taken by all values that belong to team 'D PG') and array[0][2](will be taken by all values that belong to the team 'D PP') // you will notice that all the other values for other months are at zero, thats because i want the initial values to be zero untill data has been put in for them [ [ "January", 194, 143, 19, 3, 2, 2 ], [ "February", 35, 120, 40, 30, 9, 6 ], [ "March", 0, 0, 0, 0, 0, 0 ], [ "April", 0, 0, 0, 0, 0, 0 ], [ "May", 0, 0, 0, 0, 0, 0 ], [ "June", 0, 0, 0, 0, 0, 0 ], [ "July", 0, 0, 0, 0, 0, 0 ], [ "August", 0, 0, 0, 0, 0, 0 ], [ "September", 0, 0, 0, 0, 0, 0 ], [ "October", 0, 0, 0, 0, 0, 0 ], [ "November", 0, 0, 0, 0, 0, 0 ], [ "December", 0, 0, 0, 0, 0, 0 ] ]

i use a for Loop to iterate the original Data object and then use if statements to group them by Month and Team.我使用 for 循环来迭代原始数据对象,然后使用 if 语句将它们按月份和团队分组。 But the code will become very bulky and i believe inefficient.但是代码会变得非常庞大,我认为效率低下。 Any ideas on a better way to do this关于更好的方法的任何想法

 var grouped = eval('[["January",0,0,0,0,0,0],["February",0,0,0,0,0,0],["March"0,0,0,0,0,0],["April",0,0,0,0,0,0],["May",0,0,0,0,0,0],["June"0,0,0,0,0,0],["July",0,0,0,0,0,0],["August",0,0,0,0,0,0],["September",0,0,0,0,0,0],["October",0,0,0,0,0,0],["November",0,0,0,0,0,0],["December",0,0,0,0,0,0]]'); for (i=0; i< dataResult.length; i++) { if((dataResult[i]['Month'] == 'January') && (dataResult[i]['Team'] == 'D PG')){ var original = dataResult[i]['Original_x0020_Size_x0020__x0028']; grouped[0][1] =original; } if((dataResult[i]['Month'] == 'January') && (dataResult[i]['Team'] == 'D PP')){ var original = dataResult[i]['Original_x0020_Size_x0020__x0028']; grouped[0][2] =original; } if((dataResult[i]['Month'] == 'January') && (dataResult[i]['Team'] == 'D RE')){ var original = dataResult[i]['Original_x0020_Size_x0020__x0028']; grouped[0][3] =original; } if((dataResult[i]['Month'] == 'January') && (dataResult[i]['Team'] == 'D GP')){ var original = dataResult[i]['Original_x0020_Size_x0020__x0028']; grouped[0][4] =original; } if((dataResult[i]['Month'] == 'January') && (dataResult[i]['Team'] == 'D PC')){ var original = dataResult[i]['Original_x0020_Size_x0020__x0028']; grouped[0][5] =original; } if((dataResult[i]['Month'] == 'January') && (dataResult[i]['Team'] == 'D PT')){ var original = dataResult[i]['Original_x0020_Size_x0020__x0028']; grouped[0][6] =original; }

As always, just use a hashtable to easify things:与往常一样,只需使用哈希表来简化事情:

const result = [], month = {};

// I did not want to put that identifier everywhere... :/
const wtf = "Original_x0020_Size_x0020__x0028";

for(const entry of data){
  if(month[entry.Month]){
     month[entry.Month].push(entry[wtf]);
  } else {
     result.push(month[entry.Month] = [entry.Month, entry[wtf]]);
  }
}

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

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