Advertisement
enevlogiev

Group people

Mar 20th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function Person(firstName, lastName, age) {
  3.     this.firstName = firstName;
  4.     this.lastName = lastName;
  5.     this.age = age;
  6. }
  7.  
  8. function groupBy(criteria) {
  9.     var people = [];
  10.     people[0] = new Person('Scott', 'Guthrie', 38);
  11.     people[1] = new Person('Scott', 'Johns', 36);
  12.     people[2] = new Person('Scott', 'Hanselman', 39);
  13.     people[3] = new Person('Jesse', 'Liberty', 57);
  14.     people[4] = new Person('Jon', 'Skeet', 38);
  15.  
  16.     if (criteria === 'firstName') {
  17.         var group = {};
  18.         people.forEach(function(element) {
  19.             var key = 'Group ' + element.firstName;
  20.             if (!(key in group)) {
  21.                 group[key] = [];
  22.                 group[key].push('' + element.firstName + ' '  + element.lastName + '(age' + element.age + ')');
  23.             }
  24.             else {
  25.                 group[key].push('' + element.firstName + ' '  + element.lastName + '(age' + element.age + ')');
  26.             }
  27.         });
  28.         return group;
  29.     }
  30. }
  31.  
  32. console.log(groupBy('firstName'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement