[英]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.