1
  1. I create an array of Objects from a database query and call the array jobListRecords. There are multiple records in the array created that have the same "customerID" and "name".
  2. I would like to create a new array that has one record for each customer with a new field , "jobArray", that contains another array of all of that customers services. (see sortCustomer array down below)

An example of the initial array:

jobListRecords = [
    { 
    "customerID" : "1",
    "name" : "Larry Bird",
    "serviceID" : "101",
    "serviceName" : "Dog Walking"
    },
    {
    "customerID" : "2",
    "name" : "Andrew Luck",
    "serviceID" : "202",
    "serviceName" : "Baby Sitting"
    },
    {
    "customerID" : "2",
    "name" : "Andrew Luck",
    "serviceID" : "101",
    "serviceName" : "Dog Walking"
    }
]

Desired Result

sortCustomer Example:

sortCustomer = [
   {
   "customerID" : "1",
   "name" : " Larry Bird",
   "jobArray" : [
           {
           "serviceID" : "101",
           "serviceName" : "Dog Walking"
           }
        ]
    },
    {
    "customerID" : "2",
    "name" : "Andrew Luck",
    "jobArray" : [
            {
            "serviceID" : "202",
            "serviceName" : "Baby Sitting"
            },
            {
           "serviceID" : "101",
            "serviceName" : "Dog Walking"
            }
        ]
    }

Is their a simple or efficient way of solving this without having to iterate through all the data 3+ times. Thank You for your time, below is one of the several things I tried.

I tried solving this using one example I found but it grouped all the serviceIDs together which is not what I need.

Example that DID NOT work that I tried.

jobListGrouped = _
    .chain(jobListRecords)
    .groupBy('customerID')
    .map(function(value, key) {
        return {
            CustomerID: key,
            services: _.pluck(value, 'serviceID')
        }
    })
    .value();

1 Answer 1

1

You're plucking only the serviceIDs into that array. Instead, you would need to do something like

.map(function(values, key) {
    return {
        customerID: key,
        name: values[0].name.
        services: _.map(values, _.partial(_.pick, _, 'serviceID', 'serviceName'))
    }
})

or even more explicit

.map(function(values, key) {
    return _.extend(_.omit(values[0], 'serviceID', 'serviceName'), {
        services: _.map(values, _.partial(_.pick, _, 'serviceID', 'serviceName'))
    }
})

(where that partial call is the same as function(value) { return _.pick(value, 'serviceID', 'serviceName'); })

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

3 Comments

When I try your first example it is resulting in what appears to be the structure I am looking for but the service field is an array of empty objects. The correct number of objects but they are empty. I will try the more explicit option as well.
I used the first solution but replaced the _.partial with the function(value) { return _.pick (value, 'serviceID', 'serviceName');} and this worked. Do you know why this would happen? I marked you correct.
Possibly your underscore version does not support _ as an omitted argument for partial?

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.