0

I'm new to the underscore js library and I am trying to reorganize some of my data so that it suits my application needs better. I have the following data coming in from my API:

{
    "id": "123456",
    "first_name": "John",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17,
            "name": "Following Too Closely",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4481",
        }
    ]
},
{
    "id": "123456",
    "first_name": "Jane",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17,
            "name": "Following Too Closely",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4481",
        }
    ]
}

I want to map the data so that I can pluck out the unique behaviour names, and create a new array that has the behaviours grouped. So my data would look like this:

{
    "id": "123456",
    "first_name": "John",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17
            "name": "Following Too Closely",
            "events": [
                { 
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482" 
                }
            ]
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "events": [
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482"
                },
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4481"
                }
            ]
        }
    ]
},
{
    "id": "123456",
    "first_name": "Jane",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17
            "name": "Following Too Closely",
            "events": [
                { 
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482" 
                }
            ]
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "events": [
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482"
                },
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4481"
                }
            ]
        }
    ]
}

I've currently got the following code which isn't doing a whole lot, I am stuck on this problem and I'm not sure how to go about it efficiently.

for (var i = $scope.eventData.drivers.length - 1; i >= 0; i--) {

    // Create an array containing only the unique behavior names
    var behaviourArray = _.uniq(_.pluck($scope.eventData.drivers[i].behaviours, 'name'));

    // Create a property containing a joined array of the behavior names
    $scope.eventData.drivers[i].behaviourNames = behaviourArray.join(', ');

    behaviourArray = _.map(behaviourArray, function(name) {
        return {
            'name': name,
            'events': []
        };
    });
};

I'm not sure how to push the event objects {event_id, and event_number} into the events array while matching the name of the behavior object with the new array's behavior object. Any help is appreciated.

1 Answer 1

1

The following produces the exact structure you need your data in.

here's your original thing

var data = [{
  "id": "123456",
  "first_name": "John",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4481",
  }]
}, {
  "id": "123456",
  "first_name": "Jane",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4481",
  }]
}];

the cream!!!

_.each(data, function (driver) {
  driver.behaviours = _(driver.behaviours)
    .chain()
    .groupBy(function (x) {
      return x.id;
    })
    .map(function (g) {
      return {
        id: g[0].id,
        name: g[0].name,
        events: _.map(g, function (x) {
          return {
            event_id: x.event_id,
            event_number: x.event_number
          }
        })
      };
    })
    .value();
});

do a log

console.log(JSON.stringify(data));

the following is the output

[{
  "id": "123456",
  "first_name": "John",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "events": [{
      "event_id": "01234-01234-01234",
      "event_number": "ABG4482"
    }]
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "events": [{
      "event_id ": "01234-01234-01234",
      "event_number": "ABG4482"
    }, {
      "event_id": "01234-01234-01234",
      "event_number": "ABG4481"
    }]
  }]
}, {
  "id": "123 456",
  "first_name": "Jane",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "events": [{
      "event_id": "01234-01234-01234",
      "event_number": "ABG4482"
    }]
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "events": [{
      "event_id": "01234-0123 4-01234",
      "event_number": "ABG4482"
    }, {
      "event_id": "01234-01234-01234",
      "event_number": "ABG4481"
    }]
  }]
}]
Sign up to request clarification or add additional context in comments.

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.