I'm tasked with summarizing an array of over 500 other arrays. Here is a sample of some of the entries for var list
var list = [
[
"MIKE", //employee first
"NGUYEN", //employee last
123, //id
"Sandra M.", //supervisor name
"[email protected]" //supervisor email
],
[
"MYA",
"LANE",
456,
"John A",
"[email protected]"
],
[
"RON",
"MASTER",
789,
"John A",
"[email protected]"
],
[
"MIKE",
"NGUYEN",
123,
"Sandra M.",
"[email protected]"
],
[
"MYA",
"LANE",
456,
"john A",
"[email protected]"
],
[
"ROBERT",
"RULES",
100,
"Sandra M.",
"[email protected]"
],
[
"ROBERT",
"RULES",
100,
"Sandra M.",
"[email protected]"
]
]
I think this would be a great candidate for the reduce function but I can't find a way to use it correctly.
I want to create a simple array of {} that summarizes the data into the following:
var result = [
{
supervisor: "Sandra M.", //supervisor name
email: [email protected], //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: [email protected], //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
},{})