arrays - Javascript Object Cloning in Higher Order Functions -


I am very new to javascript and I'm working through fine javascript (2) practice to learn more I am A special exercise () is giving me so much trouble.

The purpose of the exercise is death date for various things, in which according to the date of death, the date of death is different for different types of people according to the centenary. This is what I have done so far:

  function groupB (array, groupof) {var groups = {}; Array.forEach (function) {var groupName = groupOf (element); if (group name in group) group [groupName] .push (element); other group [groupName] = element;}); Return group; } Var byCentury = groupBy (Ancestor, Work (person) {return Math.ceil (person.died / 100);});  

I believe the problem can be reduced by this line: group [groupName] = element; But I do not understand why this is wrong.

Thanks for the help, if it is clear then I apologize in advance.

You need to create an array in this line

  groups [GroupName] = element;  

Instead you are specifying the object. Since the object does not have a push method, your code is failing. You can create an array like this

  groups [groupName] = [element];  

In addition, you can use shortcode sign to type the if..else position in this way

  Groups [groupName] = group [groupName] || []; Group [groupname] .push (element);  

Or, if you can type if as

 , then group name (group === incorrect) {Groups [groupName] = []; } Group [groupName] Push (element);  

Both if condition and groups [groupName] || [] Ensures that, there is an empty array in groups [groupName] , if not in groupName groups .

  group [groupName] = group [groupName] || [];   

Here, the groups [groupName] will be evaluated on undefined if not found in groupName code > Group , which is actually false since the first expression is false, the second expression [] will result from the right hand side. Therefore, if groupName is not present, then an empty array groups [groupName] .

.

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

c# - NewtonSoft JArray - how to select multiple elements with LINQ -

javascript - amcharts makechart not working -