0
var centuryGroup = grouping(ancestry, function(person){
   return Math.ceil(person.died/100);
});

function grouping(array, group){
  var groups = {};
  array.forEach(function(element) {
    var groupName = group(element);
    if(groupName in groups){
      groups[groupName].push(element); //the error is stated at this line
    }
    else{
      groups[groupName] = element;
    }
  });
  return groups;
}

As stated at the above, the error is at

groups[groupName].push(element);

If I remove this, it works.

And, if its defined as:

function grouping(array, group) {
  var groups = {};
  array.forEach(function(element) {
    var groupName = group(element);
    if (groupName in groups)
      groups[groupName].push(element);
    else
      groups[groupName] = [element];
  });
  return groups;
}

var centuryGroup = grouping(ancestry, function(person) {
  return Math.ceil(person.died / 100);
});

There is no error in the second one. *the 2nd code is taken from a tutorial.

Things I have tried:

  1. I put the "var centuryGroup = ..." below the function grouping... and still no difference
  2. I have made sure the code is working when I commented out the error line
  3. I have removed the {} from the if and else statement and the error still there (not that I expect it to have made a difference)

Exact error is

TypeError undefined is not a function

The contents of "array" that is passed in is

{ name: "Carolus Haverbeke" }
  sex:  "m"
  born: 1832
  died: 1905
  father:   "Carel Haverbeke"
  mother:   "Maria van Brussel"
{ name: "Emma de Milliano" }
  sex:  "f"
  born: 1876
  died: 1956
  father:   "Petrus de Milliano"
  mother:   "Sophia van Damme"
...

By the way, if its not supposed to have the "push" property, the 2nd code shouldn't be working. But it is and it is showing the desired result. *confused

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kiong
  • 798
  • 1
  • 8
  • 28
  • 1
    The error means that there's no `push` property of whatever `groups[groupName]` refers to. That is, your code assumes it's an array, but it's some other kind of object. The `[ ]` around `element` in the tutorial are the key. – Pointy Dec 28 '14 at 14:39
  • Out of curiosity why did you change `[element]` to `element` in the "else" statement? – Pointy Dec 28 '14 at 14:43
  • Not a direct answer, but you can use libraries like lodash or underscore which do provide common basic functionalities like groping – harishr Dec 28 '14 at 14:43
  • 1
    @HarishR wow I need to check out those libraries again – Pointy Dec 28 '14 at 14:44
  • to @Pointy, WOW. that IS the error. its supposed to be [element]. But why? – Kiong Dec 28 '14 at 14:45
  • Well `[element]` creates an array and puts the value of "element" in it, so then later you can `.push()` more values onto the end of that array. – Pointy Dec 28 '14 at 14:46
  • Thanks @Pointy. Is there any way I can close this question other than deleting it? Cause you deserve the "Answer" points as you were the one who directed me to the answer. or you put it as answers. – Kiong Dec 28 '14 at 14:54

2 Answers2

1

in else statement You are not creating new array, that's it. And anything that is not aray doesn't have push() function. Your code in else statement instead of should be:

groups[groupName] = element;

should be:

groups[groupName] = []; // Create empty array
groups[groupName].push('element'); // push an element to array

Or:

groups[groupName] = [element]; // create array with one element

P.S. Don't use if/else statements without opening/ending curly braces, You will shoot yourself in the foot one day...

if (groupName in groups) {
  groups[groupName].push(element);
} else {
  groups[groupName] = [element];
}
Bogdan Kuštan
  • 5,427
  • 1
  • 21
  • 30
  • Like I mentioned, that one without braces was from a book of tutorials and the one with braces is my code == – Kiong Dec 28 '14 at 14:52
1

It was my mistake in the

element <== in the first code within the else statement that should be [element]

It's because it is not an array and thus .push does not work. But once I put it as [element] and made it into an array, then I will be able to .push it in as an object of arrays.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kiong
  • 798
  • 1
  • 8
  • 28