简体   繁体   English

如何根据其中一个值中的数据将对象拆分为多个对象[数组中的唯一值]

[英]How to split an object into multiple objects based on data in one of it's values[unique values in an array]

I am looking to split the below data based on StudentID.我希望根据 StudentID 拆分以下数据。 I know that I have to compare each element in StudentID with every other element and push the data into a new object if we came across a new studentID,but I don't know how to implement this, as I am new to programming.我知道我必须将 StudentID 中的每个元素与其他每个元素进行比较,如果我们遇到一个新的 studentID,我必须将数据推送到一个新对象中,但我不知道如何实现这一点,因为我是编程新手。

Input data:输入数据:

data=[{"StudentID":["1","1","1","1","2","2","2","2","3","3","3","3","4","4","4","4"],
    "ModuleCode:"DES3095-N",
    "WeekNum":["1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4"],
    "Status":["p","p","pdg","p","abs","cc","p","abs","p","p","abs","p","p","abs","abs","abs"]}]

The output I am trying to achieve is:我试图实现的输出是:

[{"StudentID":1,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","pdg","p"]}
{"StudentID":2,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["abs","cc","p","abs"]}
{"StudentID":3,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","abs","p"]}
{"StudentID":4,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","abs","abs","abs"]}]

My ultimate aim here is to plot the average attendance of a student for a given Module, using D3.js.我的最终目标是使用 D3.js 绘制给定模块的学生平均出勤率。 PS The original data set is much much bigger. PS 原始数据集要大得多。

Ok, it is a bit of a strange data structure you have (data is a single object within an array - so it's not clear if this is always the case or if you expect multiple objects in that array) but let's see what we can do:好的,您拥有的数据结构有点奇怪(数据是数组中的单个对象 - 所以不清楚是否总是如此,或者您是否希望该数组中有多个对象)但让我们看看我们能做什么:

 const data = [ { "StudentID":["1","1","1","1","2","2","2","2","3","3","3","3","4","4","4","4"], "ModuleCode": "DES3095-N", "WeekNum":["1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4"], "Status":["p","p","pdg","p","abs","cc","p","abs","p","p","abs","p","p","abs","abs","abs"] }/*, { "StudentID":["1","1","1","1","2","2","2","2","3","3","3","3","4","4","4","4"], "ModuleCode": "DES4095-P", "WeekNum":["1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4"], "Status":["p","p","pdg","p","abs","cc","p","abs","p","p","abs","p","p","abs","abs","abs"] }*/ ]; /* The Goal output is: [{"StudentID":1,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","pdg","p"]}, {"StudentID":2,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["abs","cc","p","abs"]}, {"StudentID":3,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","abs","p"]}, {"StudentID":4,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","abs","abs","abs"]}] */ //The code to achieve that: function aggregateData(data){ const output = []; for (let obj of data){ let temp_arr = []; //incase we have multiple objects in our data array for (let key in obj){ if (Array.isArray(obj[key])){ //console.log('array', obj[key]) if (key == "StudentID"){ let temp_index = 0; for (let item of obj[key]){ if (item != temp_index){ temp_arr.push({"studentID": item}) temp_index = parseInt(item); } } } else{ //not studentID let temp_index = 0; for (let i = 0; i < obj[key].length; i++){ temp_index = parseInt(obj["StudentID"][i]); if (temp_arr[temp_index - 1].hasOwnProperty(key)){ temp_arr[temp_index - 1][key].push(obj[key][i]); } else { temp_arr[temp_index - 1][key] = [obj[key][i]]; } } } } else { //console.log('string', obj[key])+ for (let i = 0; i < temp_arr.length; i++){ if (temp_arr[i].hasOwnProperty(key)){ temp_arr[i][key].push(obj[key]); } else { temp_arr[i][key] = obj[key]; } } } } output.push(temp_arr); } return output; } console.log(aggregateData(data));

Output is:输出是:

[
  [
    {"StudentID":1,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","pdg","p"]},
    {"StudentID":2,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["abs","cc","p","abs"]},
    {"StudentID":3,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","abs","p"]},
    {"StudentID":4,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","abs","abs","abs"]}
  ]
]

Note it is wrapped in one more array than you wanted in your question because it is not clear if your data array will have more than one element or not (ie maybe for another module code or something), so I made it as general as possible.请注意,它包含在比您在问题中想要的多一个数组中,因为尚不清楚您的数据数组是否包含多个元素(即可能用于另一个模块代码或其他内容),因此我将其设置得尽可能通用.

For example if we have this as the input:例如,如果我们将其作为输入:

const data = [
    {
    "StudentID":["1","1","1","1","2","2","2","2","3","3","3","3","4","4","4","4"],
    "ModuleCode": "DES3095-N",
    "WeekNum":["1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4"],
    "Status":["p","p","pdg","p","abs","cc","p","abs","p","p","abs","p","p","abs","abs","abs"]
    },
    {
    "StudentID":["1","1","1","1","2","2","2","2","3","3","3","3","4","4","4","4"],
    "ModuleCode": "DES4095-P",
    "WeekNum":["1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4"],
    "Status":["p","p","pdg","p","abs","cc","p","abs","p","p","abs","p","p","abs","abs","abs"]
    }
  ];

Then we get this as the output (ie an array for each moduleCode):然后我们将其作为输出(即每个模块代码的数组):

[
  [
    {"StudentID":1,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","pdg","p"]},
    {"StudentID":2,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["abs","cc","p","abs"]},
    {"StudentID":3,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","p","abs","p"]},
    {"StudentID":4,"ModuleCode:"DES3095-N","WeekNum":["1","2","3","4"],"Status":["p","abs","abs","abs"]}
  ],
  [
    {"StudentID":1,"ModuleCode:"DES4095-P","WeekNum":["1","2","3","4"],"Status":["p","p","pdg","p"]},
    {"StudentID":2,"ModuleCode:"DES4095-P","WeekNum":["1","2","3","4"],"Status":["abs","cc","p","abs"]},
    {"StudentID":3,"ModuleCode:"DES4095-P","WeekNum":["1","2","3","4"],"Status":["p","p","abs","p"]},
    {"StudentID":4,"ModuleCode:"DES4095-P","WeekNum":["1","2","3","4"],"Status":["p","abs","abs","abs"]}
  ]
]

We can possibly do this cleaner by converting to a Map first and then using some map filter reduce functions but this does the job too.我们可以通过先转换为 Map 然后使用一些 map filter reduce 函数来做到这一点,但这也可以做到这一点。

暂无
暂无

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

相关问题 根据相似的值将对象拆分为对象数组 - Split an object in to array of objects based on similar values 如何将一个对象数组拆分成多个普通数组,其中每个数组都由一个属性的值填充? - How to split an array of objects into multiple normal arrays where each array is populated by one property's values? 如何将一个包含数组的 object 拆分为多个对象 - How to split one object with an array in it to multiple objects 如何根据对象的值将对象数组拆分为另一个数组? - How to split an Array of Objects into another Arrays based on their values? 根据属性将一个对象拆分为多个对象 - split one object into multiple objects based on the property 如何使用这些键及其值将具有一个 object 和多个键的数组转换为多个对象的数组? - How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values? 如何对数组多个对象的唯一属性的字段值求和? - How to sum values of fields for a unique property of an array multiple objects? 为数组中的多个但相同的对象附加唯一值 - Attach unique values for multiple, but identical objects in an array Lodash:如何从对象数组中获取唯一值,然后按这些唯一值对这些对象进行排序? - Lodash: How to get unique values from an object array and then sort those objects by those unique values? 如何根据Angular 5中的数组中的多个列获取唯一值? - How to Get unique values based on multiple columns from an array in Angular 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM