Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given an array of objects how do I sort the objects by a property in depth n

I have an array of objects that I wish to sort by some grouping property data and a string telling me which property to group by(eg: 'Organization' or 'Organization.Name')

I need to write a function which takes in data that looks like beforeData and returns afterData

Input :

beforeData = [
{'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]},
{'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':14, 'LongName': 'Group C'}]},
{'name':'Kristine Bell', 'Id':3, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]},
{'name':'Adrian P', 'Id':4, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]}
]

Output:

    afterData = [
    {   
        'Group': 'Group A', 
        'entities':[
            {'name':'Adrian P', 'Id':4, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]},
            {'name':'Kristine Bell', 'Id':3, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]},
            {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]}]
    },
    {   
        'Group': 'Group B', 
        'entities':[
            {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]},
            {'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':13, 'LongName': 'Group C'}]},]
    },
    {   
        'Group': 'Group C', 
        'entities':[
            {'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':13, 'LongName': 'Group C'}]},]
    }
]

How would I go about accomplishing this? My current attempts are extremely bloated and take forever given large sets of data.

Special Kicker! : the function that solves this issue needs to be able to solve it without knowing beforehand whether the "group by property" is in depth 1 or 2(eg: 'Organization' or 'Organization.LongName').

like image 304
CodePrimate Avatar asked Jun 27 '14 06:06

CodePrimate


1 Answers

Something from me:

// this function performs data extraction from an object
// the first argument is a name of the property to be extracted
// it might be just a 1st level deep value like `name`
// or nested like `foo.bar.baz`
// in case if one of intermediate items is an array - an array of
// results is returned
function dot(name, obj) {
    if (!name) {
        return obj;
    }

    var match = name.match(/^([^.]+)(?:\.(.*))?$/),
        head = match[1],
        tail = match[2];

    if (Array.isArray(obj)) {
        return obj.map(function(item) {
            return dot(name, item);
        });
    }

    if (obj === null || typeof obj != 'object') {
        return null;
    }

    return dot(tail, obj[head]);
}

// this function accepts an array of data and a key to group by
// as a result it returns an object with keys equal to a group by key
// and values that hold that key
function groupBy(data, key) {
    return data.reduce(function(result, item) {
        var keys = dot(key, item);
        if (!Array.isArray(keys)) {
            keys = [keys];
        }

        keys.forEach(function(key) {
            if (!(key in result)) {
                result[key] = [];
            }

            result[key].push(item);
        });

        return result;
    }, {});
}

console.log(groupBy(beforeData, 'Organizations.LongName'));

JSFiddle: http://jsfiddle.net/w8N4j/

It can be now easily reformatted to any other format you want.

Eg to get the exact format from the question here is a tiny transformer:

function transformerExample(hash) {
    var result = [];
    for (var key in hash) if (hash.hasOwnProperty(key)) {
        result.push({
            Group: key,
            entities: hash[key]
        });
    }

    return result;
}

PS: the main implementation obviously may not handle all possible errors. Depending on the actual requirements it is not hard to improve it.

like image 88
zerkms Avatar answered Oct 19 '22 04:10

zerkms