Shrink an array into a group of smaller arrays using underscore.js
// Make sure to include underscore.js
_.mixin({
chunk : function (array, unit) {
if (!_.isArray(array)) return array;
unit = Math.abs(unit);
var results = [],
length = Math.ceil(array.length / unit);
for (var i = 0; i < length; i++) {
results.push(array.slice( i * unit, (i + 1) * unit));
}
return results;
}
});
Example:
JSON.stringify( _.chunk([1,2,3,4,5,6,7,8,9], 2) );
// returns "[[1,2],[3,4],[5,6],[7,8],[9]]"
Here's the same thing without using Underscore.js
var chunk = function (array, unit) {
if (typeof array !== "object")
return array;
unit = Math.abs(unit);
var results = [],
length = Math.ceil(array.length / unit);
for (var i = 0; i < length; i++) {
results.push(array.slice(i * unit, (i + 1) * unit));
}
return results;
}
What REALLY is Data Science? Told by a Data Scientist - By Joma Tech
Writing perfect code is a challenging process. That's where code reviews come in to help…
"The Next Leap: How A.I. will change the 3D industry - Andrew Price - Blender"
"Captain Disillusion: World's Greatest Blenderer - Live at the Blender Conference 2018 - CaptainDisillusion"
My 5 Favorite Linux Shell Tricks for SPEEEEEED (and efficiency) - By tutoriaLinux > What's…
View Comments
Here is a scenario. I have a contact list of people such as
Location Section Last Name First Name phone number
St Loius Sales Jones Sam 123456789
St Loius HR Smith James 123456789
St Loius HR Crandal Eric 123456789
Jacksonville Manager Brown Steven 123456789
Jacksonville Driver Moeller Susan 123456789
St Loius Sales Cruz Jennifer 123456789
Ft Myers Sales Taylor David 123456789
Ft Myers Sales Goode Kirk 123456789
Ft Myers HR Farmer Anne 123456789
I am buildign a table with the above data, appending to an array the elements such as and data.
I want my output to be grouped first on the location, then on section
Ft Myers
HR
Farmer Anne 123456789
Sales
Taylor David 123456789
Goode Kirk 123456789
Jacksonville
Manager
Brown Steven 123456789
Driver
Moeller Susan 123456789
I have been trying to work this out for a week now. Grouping on one item is easy, but two groupings is escaping me. Any Ideas?
Hey Eric,
You can solve this problem by using .reduce() and .map().
Here's a quick write up I did assuming the input is stream of comma separated fields.
//location section lastName firstName phoneNumber
var csv = [
"St Loius,Sales,Jones,Sam,123456789",
"St Loius,HR,Smith,James,123456789",
"St Loius,HR,Crandal,Eric,123456789",
"Jacksonville,Manager,Brown,Steven,123456789",
"Jacksonville,Driver,Moeller,Susan,123456789",
"St Loius,Sales,Cruz,Jennifer,123456789",
"Ft Myers,Sales,Taylor,David,123456789",
"Ft Myers,Sales,Goode,Kirk,123456789",
"Ft Myers,HR,Farmer,Anne,123456789"
].sort();
var list = csv.map(function(o){
return o.split(",");
});
// adds first element as a property to the object and appends to the rest of the array to the property value.
// Not sure what to call this function ...
var getUniqueFirstFn = function(a, b){
var location = b.shift();
a[ location ] = a[ location ] || [];
a[ location ].push( b );
return a;
}
var groupedByLocations = list.reduce(getUniqueFirstFn, {});
var groupedByLocations = Object.keys(groupedByLocations).reduce(function(a, prop){
a[prop] = groupedByLocations[prop].reduce(getUniqueFirstFn, {});
return a;
}, {});
JSON.stringify( groupedByLocations, null, 2 );
Output:
{
"Ft Myers": {
"HR": [
[
"Farmer",
"Anne",
"123456789"
]
],
"Sales": [
[
"Goode",
"Kirk",
"123456789"
],
[
"Taylor",
"David",
"123456789"
]
]
},
"Jacksonville": {
"Driver": [
[
"Moeller",
"Susan",
"123456789"
]
],
"Manager": [
[
"Brown",
"Steven",
"123456789"
]
]
},
"St Loius": {
"HR": [
[
"Crandal",
"Eric",
"123456789"
],
[
"Smith",
"James",
"123456789"
]
],
"Sales": [
[
"Cruz",
"Jennifer",
"123456789"
],
[
"Jones",
"Sam",
"123456789"
]
]
}
}