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"
}
]
entriescontains & also tell us in a few words what the expected output should be? or what exactly you're trying to do?