javascript - How to put items into grouped arrays where grouped by a particular key -


I am very new to the object in JS, so I'll need some help. I have the following array:

  var person = [{fname: 'steven', lname: 'Jameson', age: 20}, {fname: 'George', lname: 'thrump' , Age: 40}, {fname: 'Peter', lname: 'Jacobs', age: 60}, {fname: 'Peter', lname: 'thrump', age: 20}, {fname: 'steven', lname : 'Henderson', age: 40}, {fname: 'John', lname: 'yakub', age: 30}, {fname: 'steven', lname: 'thrump', age: 50}, {fname: George ', Lname:' Peterson ', age: 40}, {fname:' Peter ', lname:' Jameson ', age: 60}];   

I need to sort the information from a property I have tried with the function (array, 'property') in my case this The function will be (individuals, 'fname') . I have to get an object with the key of different names and to store that key with everyone about that name. For example:

  object {Steven: First name Steven, everyone with George: First name George, everyone with Peter: First name Peter, John: Everyone first Name John}   

I get it:

  function group (array, property) {var object = {}; For (var i = 0; i & lt; persons.length; i ++) {Object [person [i] [property]] = []; Object [person [i] [property]]. Push (person [i])}; };   

Create the object as a key with different names, and assign them an array. And then I'm stuck and do not know what to do next? Anyone can help and if there is a few minutes to explain it a little bit.

Thanks in advance.

The only issue is with this line:

  object [person [ I] [property]] = [];   

It executes for each iteration, resets each Array to empty before adding 1 component. Before setting this setting, a condition is required:

  if (objects [i] [property] in object) object [person [i] [property] = []; Object [individuals [i] [property] .push (individuals [i]);    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

jsp - Google line chart x-axis shrinks on transition -

java - Reaching JTextField in a DocumentListener -