0

My goal is to start with a JSON object. This is not the Object I will be working with, but it will do for the example.

 var json = [
    {"id":"1","age":"20", "region":"Y"},
    {"id":"2", "age":"20", "region":"X"},
    {"id":"3", "age":"31", "region":"Z"},
    {"id":"4", "age":"35", "region":"Q"}
];

Lets say my goal is to sort the region based off of the age. I wish to create a function that will split json into this:

var newObj = [
              [
                {"id":"1","age":"20", "region":"Y"},
                {"id":"2", "age":"20", "region":"X"}
              ],
              [
                {"id":"3", "age":"31", "region":"Z"}
              ],
              [
               {"id":"4", "age":"35", "region":"Q"}
              ]
            ];

Then I could sort the new arrays. I will never know what the ages will be or how many arrays I will need to be created. Thank you for your help.

jamesRH
  • 420
  • 3
  • 12
  • 1
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). I also don't understand what your problem is exactly. `[]` creates a new array, and you can create as many as you want. To repeat certain instructions, you can use a loop. – Felix Kling Jan 27 '15 at 22:28
  • I apologize for not being specific enough: this variable written in the form of JSON that is an instanceof Object. Do you have a suggestion for an answer? – jamesRH Jan 27 '15 at 22:38
  • 1
    I still don't know what your specific problem is. Maybe this helps: http://stackoverflow.com/q/11922383/218196 . – Felix Kling Jan 27 '15 at 22:41
  • I am trying to think of a way to write a function/method that could for instance take the json, age, and region as arguments. It would then create nested arrays inside of var newObj based off of age. Then I could sort based off of the region if there was many to sort from. Where I am stuck is I can't think of how to create these new arrays. I think a solution lies in a variation of merge sort where only similar ages get merged back together, but I wanted to see if anyone had an easier idea before I can up with that. – jamesRH Jan 27 '15 at 22:56
  • You can have a `age -> array` table as intermediate data structure. When iterating over all elements in the source array, for each age you check whether it exists in the table or not. If yes, append it to the already existing array. If not, create a new entry with a new array (containing the current object). You can use an object to implement such a table. Then in the end you can iterate over all properties of that (table) object and push them to a new array (to get an array of arrays). – Felix Kling Jan 27 '15 at 22:59

2 Answers2

2

It would be easy using underscore.js:

 var items = [
    {"id":"1","age":"20", "region":"Y"},
    {"id":"2", "age":"20", "region":"X"},
    {"id":"3", "age":"31", "region":"Z"},
    {"id":"4", "age":"35", "region":"Q"}
];

var ageGroups = _.groupBy( items, "age" );

http://jsfiddle.net/w7s51hng/

If you don't want to include a library the nice thing about underscore is that it has annotated source code so you can see how it's implemented: http://underscorejs.org/docs/underscore.html#section-36

pawel
  • 35,827
  • 7
  • 56
  • 53
  • Thank you! That will work nicely. I was also going to ask that if a suggestion was from a library please make it underscore so I could easily get it out of the code! – jamesRH Jan 27 '15 at 23:03
1

I think you want this:

function groupByProperty(list, prop) {
    var hash = {};
    list.forEach(function(obj) {
        var value = obj[prop];

        if (!(value in hash)) {
            hash[value] = [];
        }

        hash[value].push(object);
    });

    return Object.keys(hash).map(function(key) {
        return hash[key];
    });
}

var newObj = groupByProperty(json, 'age');
alexpods
  • 47,475
  • 10
  • 100
  • 94