[英]summarize array to an Object using reduce function
I'm tasked with summarizing an array of over 500 other arrays.我的任务是总结超过 500 个其他数组的数组。 Here is a sample of some of the entries for
var list
这是
var list
的一些条目的示例
var list = [
[
"MIKE", //employee first
"NGUYEN", //employee last
123, //id
"Sandra M.", //supervisor name
"sandra.m@email.com" //supervisor email
],
[
"MYA",
"LANE",
456,
"John A",
"john.a@email.com"
],
[
"RON",
"MASTER",
789,
"John A",
"john.a@email.com"
],
[
"MIKE",
"NGUYEN",
123,
"Sandra M.",
"sandra.m@email.com"
],
[
"MYA",
"LANE",
456,
"john A",
"john.a@email.com"
],
[
"ROBERT",
"RULES",
100,
"Sandra M.",
"sandra.m@email.com"
],
[
"ROBERT",
"RULES",
100,
"Sandra M.",
"sandra.m@email.com"
]
]
I think this would be a great candidate for the reduce function but I can't find a way to use it correctly.我认为这将是 reduce 函数的一个很好的候选者,但我找不到正确使用它的方法。
I want to create a simple array of {}
that summarizes the data into the following:我想创建一个简单的
{}
数组,将数据汇总为以下内容:
var result = [
{
supervisor: "Sandra M.", //supervisor name
email: sandra.a@email.com, //supervisor email
employees: 2, //number of employees supervised by Sandra
entries: 4 //total number of items in array with Sandra as the supervisor
},
{
supervisor: "John A.", //supervisor name
email: john.a@email.com, //supervisor email
employees: 2, //number of employees supervised by John
entries: 3 //total number of items in array with John as the supervisor
}
]
Here is where I got stuck:这是我卡住的地方:
var result= list.reduce(function(all,item){
all[item[3]] = all[item[3]] || []
all[item[3]].push({
supervisor: item[3],
email: item[4],
employees: item[2]++,
entries: item[0]++,
})
return all
},{})
The main difficulty here looks to be differentiating an employee from a (non-employee) entry.这里的主要困难似乎是将员工与(非员工)条目区分开来。 It looks like all non-employee entry items have 6 items in an input array (including a value for the month), whereas the employees do not have a month.
看起来所有非员工条目项目在输入数组中都有 6 个项目(包括月份的值),而员工没有月份。 This can be identified pretty easily by checking the length of what you're iterating over.
通过检查您正在迭代的内容的长度,可以很容易地识别出这一点。
Reduce into an object indexed by the supervisor email, creating an object at that point if it doesn't exist beforehand.减少为由主管电子邮件索引的对象,如果它事先不存在,则在该点创建一个对象。 Then, if the item you're iterating over is an employee, increment the
employees
property (and increment the entries
property regardless).然后,如果您要迭代的项目是雇员,则增加
employees
属性(无论如何都增加entries
属性)。
var list = [ [ "MIKE", //employee first "NGUYEN", //employee last 123, //id "Sandra M.", //supervisor name "sandra.m@email.com" //supervisor email ], [ "MYA", "LANE", 456, "John A", "john.a@email.com" ], [ "RON", "MASTER", 789, "John A", "john.a@email.com" ], [ "MIKE", "NGUYEN", 123, "Sandra M.", "sandra.m@email.com" ], [ "MYA", "LANE", 456, "February", "john A", "john.a@email.com" ], [ "ROBERT", "RULES", 100, "March", "Sandra M.", "sandra.m@email.com" ], [ "ROBERT", "RULES", 100, "March", "Sandra M.", "sandra.m@email.com" ] ] var resultObj = list.reduce(function(all,item){ const isEmployee = item.length === 5; const [supervisor, email] = item.slice(-2); if (!all[email]) { all[email] = { supervisor, email, employees: 0, entries: 0 }; } if (isEmployee) { all[email].employees++; } all[email].entries++; return all },{}); const result = Object.values(resultObj); console.log(result);
For the new data structure, make a Set of the ids that have been counted as employees.对于新的数据结构,制作一组已经算作员工的id。 If an item you're iterating over is included in that Set, don't add to the employees count:
如果您要迭代的项目包含在该 Set 中,请不要添加到员工数中:
var list = [ [ "MIKE", //employee first "NGUYEN", //employee last 123, //id "Sandra M.", //supervisor name "sandra.m@email.com" //supervisor email ], [ "MYA", "LANE", 456, "John A", "john.a@email.com" ], [ "RON", "MASTER", 789, "John A", "john.a@email.com" ], [ "MIKE", "NGUYEN", 123, "Sandra M.", "sandra.m@email.com" ], [ "MYA", "LANE", 456, "john A", "john.a@email.com" ], [ "ROBERT", "RULES", 100, "Sandra M.", "sandra.m@email.com" ], [ "ROBERT", "RULES", 100, "Sandra M.", "sandra.m@email.com" ] ] const seenIds = new Set(); var resultObj = list.reduce(function(all,item){ const [id, supervisor, email] = item.slice(-3); if (!all[email]) { all[email] = { supervisor, email, employees: 0, entries: 0 }; } if (!seenIds.has(id)) { all[email].employees++; seenIds.add(id); } all[email].entries++; return all },{}); const result = Object.values(resultObj); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.