0

I have an array of objects from a database that I want to collapse for succinctness. I've imported underscore.js to help the process, and am unsuccessfully trying to use the 'groupBy' function provided to condense the following list:

[
    {ID:2570,name:"jim",latitude:59.4,longitude:-7.29},
    {ID:2573,name:"joe",latitude:54.4,longitude:-7.36},
    {ID:2573,name:"joe",latitude:54.3,longitude:-7.37},
    {ID:2574,name:"bob",latitude:58.4,longitude:-7.31},
    {ID:2574,name:"bob",latitude:58.6,longitude:-7.38},
    {ID:2574,name:"bob",latitude:58.8,longitude:-7.39},
    {ID:2575,name:"mary",latitude:54.1,longitude:-7.30},
]

To the format:

[
    {ID:2570, name:"jim", locs : [[59.4,-7.29]]},
    {ID:2573, name:"joe", locs : [[54.4,-7.36], [54.3,-7.37]]} //etc...
]
1
  • show how you are doing group by? Commented Nov 5, 2014 at 13:21

3 Answers 3

2

Following your intuition to use _.groupBy,

var data = [
    {ID:2570,name:"jim",latitude:59.4,longitude:-7.29},
    {ID:2573,name:"joe",latitude:54.4,longitude:-7.36},
    {ID:2573,name:"joe",latitude:54.3,longitude:-7.37},
    {ID:2574,name:"bob",latitude:58.4,longitude:-7.31},
    {ID:2574,name:"bob",latitude:58.6,longitude:-7.38},
    {ID:2574,name:"bob",latitude:58.8,longitude:-7.39},
    {ID:2575,name:"mary",latitude:54.1,longitude:-7.30},
];
    
var groups = _.groupBy(data, function(locationObject) { return locationObject.ID; });
    
var result = _.map(groups, function(group) {
    var condensed = {ID: group[0].ID, name: group[0].name};
    condensed.locs = _.map(group, function(row) { 
        return [row.latitude, row.longitude]; 
    }); 
    return condensed;
});

document.getElementById('log').innerHTML = JSON.stringify(result, null, '  ');
<script src="http://underscorejs.org/underscore-min.js"></script>
<pre id="log"></pre>

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

Comments

0

I think that you can use _.reduce method this way:

_.reduce(data, function(prev, curr) {
    var obj = _.findWhere(prev, {ID: curr.ID});
    if (obj) {
        obj.locs.push([curr.latitude, curr.longitude]);
    }
    else {
        curr.locs = [[curr.latitude, curr.longitude]];
        prev.push(_.omit(curr, ['latitude', 'longitude']));
    }
    return prev;
}, []);

Demo: http://jsfiddle.net/yd1Lc3ws/1/

Comments

0
var 
    input = [
        {ID:2570,name:"jim",latitude:59.4,longitude:-7.29},
        {ID:2573,name:"joe",latitude:54.4,longitude:-7.36},
        {ID:2573,name:"joe",latitude:54.3,longitude:-7.37},
        {ID:2574,name:"bob",latitude:58.4,longitude:-7.31},
        {ID:2574,name:"bob",latitude:58.6,longitude:-7.38},
        {ID:2574,name:"bob",latitude:58.8,longitude:-7.39},
        {ID:2575,name:"mary",latitude:54.1,longitude:-7.30},
    ]
    , output = {} //Handles filtering
    , outputArray = [] //Not mandatory, if you want output to be an array
    ;
for(var key,i=-1;++i<input.length;){
    key = input[i].ID; //if ID is unique and ID/name combination is always same
    //key = input[i].ID + input[i].name; //if not
    if(!output[key]){
        output[key] = {ID : input[i].ID, name : input[i].name, locs : []};
        outputArray.push(output[key]);
    }
    output[key].locs.push([input[i].latitude, input[i].longitude]);
}

console.dir(output);
console.dir(outputArray);

Comments

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.