1

This is a continuation from a previous question in regards to grouping. I have a file of structure below and what I want to do rather than simply isolating one id I want to loop through the entire array (imagine 1000+ ids) and create separate arrays for each which I can then do further work on. So in the example below id1 would be group together in one array and id2 would be grouped together in another array. Once I had separated each ID into a separate array I would then continue to filter each further based on a set of conditions.

[{col1: 'id1', col2: '123', col3: '12/01/12'},
{col1: 'id1', col2: '100', col3: '12/01/12'},
{col1: 'id2', col2: '-100', col3: '12/01/12'},
{col1: 'id2', col2: '123', col3: '13/01/12'}]

.

Any advice on the best way to break the array down and then how to call individual ID arrays would be much appreciated.

Thanks in advance

3
  • How would the desired result look like? Any advice on the best way to break the array down... seems a bit philosophical? Best in what? Performance? Readability? Commented Apr 29, 2017 at 18:02
  • Can you provide the expected output format? Commented Apr 29, 2017 at 18:30
  • @ibrahimmahrir by best I meant actually gets the job done! Will be sure to frame my question better next time @ Pankaj - just different arrays that are associated with an individual ID - in the real data set a ID may have 100's of rows associated with it which I then want to do further manipulation on. Both solutions offered below do the job nicely. Commented Apr 29, 2017 at 22:50

2 Answers 2

3

You could use the given id as key for an object and collect the items in own array.

var data = [{ col1: 'id1', col2: '123', col3: '12/01/12' }, { col1: 'id1', col2: '100', col3: '12/01/12' }, { col1: 'id2', col2: '-100', col3: '12/01/12' }, { col1: 'id2', col2: '123', col3: '13/01/12' }],
    object = data.reduce(function (r, o) {
        r[o.col1] = r[o.col1] || [];
        r[o.col1].push(o);
        return r;
    }, Object.create(null));
    
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

Thanks - did exactly what I needed - if you have a moment and could explain the method it would be much appreciated just for my own learnings
1

If you want to have the array of arrays of same ids grouped together, try below code.

var a  = [{col1: 'id1', col2: '123', col3: '12/01/12'},
{col1: 'id1', col2: '100', col3: '12/01/12'},
{col1: 'id2', col2: '-100', col3: '12/01/12'},
{col1: 'id2', col2: '123', col3: '13/01/12'}];
var currentID = a[0].col1;
var group = [];
var collectionOfIDs = [];
a.forEach(function(v, i) {
  //console.log(i,v);
  if (currentID == v.col1) {
    collectionOfIDs.push(v);
  } else {
    group.push(collectionOfIDs);
    currentID = v.col1;
    collectionOfIDs = [v];
  }
});
group.push(collectionOfIDs);
console.log(group)

6 Comments

The above code is useful if you want to have control over structure of grouped data & if its needed then use it else go with the solution given by @Nina if that structure is ok for the further solution.
Thanks - both work really well - can you explain how this is different from Ninas? Both appear to give me control over the newly group data.
By 'control over structure', I meant while creating the grouped data itself, you can tweak the structure to whatever you want, like having array of arrays or object of objects with keys being ids. for more info about Ninas answer refer reduce API - developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
Thanks again - really appreciate the help
@Vinayaki - as a follow up: If I try and access the data sitting at id1 I can access all data associated with that id but if I want to get just get all col2 values associated with that id I get undefined. I tried the following: console.log(group[1].col2); The alternative is: console.log(group[1][1].col2) But this then only gives me one element of the grouped array. When I would like to log all data associated with that col. Is there an effective way to do this or would I have to create a loop which looped through each ID and then each array within that ID array? Thanks!
|

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.