0

I've been trying different methods but none of them work but this one seems to be the most simple to do the task that is to find the entries that are within a group ( idGroup ) wrap them into another array ( inside entries ) and delete the entry that is outside the array it belongs

for(entry in entries) {

if (entries[entry].idGroup) {

    var found = _.find(entries, function (num) { if(num.group == entries[entry].idGroup) { return num; } });

    if(found) {

        entries[_.indexOf(entries, found)].entries.push(entries[entry]);
        delete entries[entry];
        console.log('pushed');

    }else{

        entries.splice(entry,0, { group: entries[entry].idGroup, entries: [entries[entry]] });
        delete entries[entry];
        console.log('created');
    }
}
}

This scripting seems to be the most simple of all i've tried but it stills dosen't work; after deleting the current entry it seems to cause a error with the underscore library function _.find "TypeError: Cannot read property 'group' of undefined" at the line which _.find is, if i remove the delete operator it works fine.

This is how entries STRUCTURE looks like before

[
  {

    id: 0,
    idGroup: 1,
    data: "example"
  },
  {
    id: 1,
    idGroup: null,
    data: "example"
  },
  {
    id: 2,
    idGroup: 1,
    data: "example"
 }
]

and after ( as it should )

[
  {

    id: 0,
    group: 1,
    entries: [
     {

       id: 0,
       idGroup: 1,
       data: "example"
     },
     {
       id: 2,
       idGroup: 1,
       data: "example"
     }
    ]
  },
  {
    id: 1,
    idGroup: null,
    data: "example"
 }
]
5
  • 1
    This is very confusing. Can you show us what entries contains & also tell us in a few words what the expected output should be? or what exactly you're trying to do? Commented Nov 9, 2014 at 17:10
  • 2
    Modifying a list you are currently iterating isn't so nice even if underscore weren't a functional library. How about starting from _.groupBy then maybe each or map? Commented Nov 9, 2014 at 17:35
  • @istos explained it better, sorry. Commented Nov 9, 2014 at 17:56
  • Not entirely sure what you want but take a look at some of the set functions that underscore provides e.g. union, intersection and difference. Commented Nov 9, 2014 at 17:59
  • @GruffBunny updated it again with how it should look after, sorry hehe, also thanks lossleader i'll take a look but i'd be also grateful if you gave me a example. Commented Nov 9, 2014 at 18:01

1 Answer 1

1
arr = [
  {

    id: 0,
    idGroup: 1,
    data: "example"
  },
  {
    id: 1,
    idGroup: null,
    data: "example"
  },
  {
    id: 2,
    idGroup: 1,
    data: "example"
 }
];

b = _.groupBy(arr, function(a) {return a.idGroup; });
c = _.toArray(b);
d = _.map(c, function(es, i) { return (es.length > 1) ? {id: i, group: 1 , entries: es} : _.extend({id: i}, es[0]) });

Or better if you like

_.chain(arr)
  .groupBy(function(a) {return a.idGroup; })
  .toArray()
  .map(function(es, i) { 
    return (es.length > 1) ? {id: i, group: 1 , entries: es} : _.extend({id: i}, es[0]); 
  })
  .value()

groupBy transforms your original array of Entries to an object whose keys are idGroup and values are the array of Entries that matched the criteria.

toArray transforms the new object to an array of array of Entries: ([[Entry]])

map transforms each array of Entry ([Entry]) (that is a member of [[Entry]]) to either a new object (with group: 1 and entries: [Entry] or the original Entry but with addition of an id field using extend).

Sign up to request clarification or add additional context in comments.

8 Comments

Many Thanks! i'll just see if it works fine here before accepting the answer.
It worked wonderfully! but only with one group, if there's a entry with a idGroup other than 1 it will not be added to a group array, may i ask you if you could solve that?
I'm sorry but actually could you solve the issue with the multiple groups? because the script in my main question have this ability. ( also i can't seem to fix that myself )
@Bud can you again post your input and expected output?
i just created another question because of certain issues with the code in your answer, also because i better structured my question/issue so it may be more helpful to others. stackoverflow.com/questions/26850076/…
|

Your Answer

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