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

7
  • 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. Commented Dec 28, 2014 at 14:39
  • Out of curiosity why did you change [element] to element in the "else" statement? Commented Dec 28, 2014 at 14:43
  • Not a direct answer, but you can use libraries like lodash or underscore which do provide common basic functionalities like groping Commented Dec 28, 2014 at 14:43
  • 1
    @HarishR wow I need to check out those libraries again Commented Dec 28, 2014 at 14:44
  • to @Pointy, WOW. that IS the error. its supposed to be [element]. But why? Commented Dec 28, 2014 at 14:45

2 Answers 2

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];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Like I mentioned, that one without braces was from a book of tutorials and the one with braces is my code ==
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.