What is partition() in underscore.js and how to use it

Saturday, 7 January 2017




  • This method takes an array or a collection and predicate ( an anonymous function for prediction ) and split it into two arrays. The first array contains the elements which satisfy the predicate, the second array contains the element which doesn't satisfy the predicate.
  • By using this method we can do filtration on an array or a collection.
Syntax :

_.partition(array, predicate)


Where predicate is an anonymous function which takes each element of the array and match with the predict condition and returns the matched element, e.g


predicate = function(element){

return typeof element == "number";
} 

Examples :


In this example we have the following list of employees, each employee has their name, department, and salary.  

[
 { "name": "Jhon", "dept": "Sales","sal": 10000, "gender": "Male" },
 { "name": "Smith", "dept": "Sales", "sal": 12000, "gender": "Male" },
 { "name": "Alex", "dept": "Marketing", "sal": 10000, "gender": "Male" },
 { "name": "Andrew", "dept": "Marketing", "sal": 15000, "gender": "Male" },
 { "name": "Marie", "dept": "Engineering", "sal": 18000, "gender": "Female" },
 { "name": "Philippe", "dept": "Engineering", "sal": 8000, "gender": "Male" }
]
Following are the requirements,

1. Group the employees by respected department.

2. Group the employees whose salary > 10000, < 10000, > 15000
3. Group the employees by their gender

Following is the logic and coding to get above requirements,

var personList = [
  { "name": "Jhon", "dept": "Sales","sal": 10000, "gender": "Male" },
  { "name": "Smith", "dept": "Sales", "sal": 12000, "gender": "Male" },
  { "name": "Alex", "dept": "Marketing", "sal": 10000, "gender": "Male" },
  { "name": "Andrew", "dept": "Marketing", "sal": 15000, "gender": "Male" },
  { "name": "Marie", "dept": "Engineering", "sal": 18000, "gender": "Female" },
  { "name": "Philippe", "dept": "Engineering", "sal": 8000, "gender": "Male" }
 ];

var salesDept = _.partition(personList, function(element){
 return element.dept == "Sales"; 
})[0];
var marketingDept = _.partition(personList, function(element){
 return element.dept == "Marketing"; 
})[0];
var enggDept = _.partition(personList, function(element){
 return element.dept == "Engineering"; 
})[0];

var [salGreat15000, salGreat10000, salLess10000] = [
 _.partition(personList, function(element){
  return element.sal > 15000; 
 })[0],
 _.partition(personList, function(element){
  return element.sal > 10000; 
 })[0],
 _.partition(personList, function(element){
  return element.sal < 10000; 
 })[0]
];

var maleEmpList =  _.partition(personList, function(element){
 return element.gender == "Male"; 
})[0];;
var feMaleEmpList =  _.partition(personList, function(element){
 return element.gender == "Female"; 
})[0];

console.log("Sales Department", salesDept);

console.log("Marketing Department", marketingDept);

console.log("Engineering Department", enggDept);

console.log("Emmployee list with gender male", maleEmpList);

console.log("Emmployee list with gender female", feMaleEmpList);

console.log("Emmployee list with salary > 15000", salGreat15000);

console.log("Emmployee list with salary > 10000", salGreat10000);

console.log("Emmployee list with salary < 10000", salLess10000);
This is about partition in underscorejs. If you have any doubt please feel free to comment down below. If you have any suggestion to this post please mention in below as comment.

No comments:

Post a Comment

 
About Contact Career Advertise Job Support Online Training Acardemic Projects Internship
Copyright © 2016. [ info ].
Design by Herdiansyah Hamzah. & Distributed by Free Blogger Templates
Creative Commons License