Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have just got introduced to underscore.js/lodash and finding it a power tool to manipulating JSON objects. However my understanding is limited at the moment and need to some help with this JSON object.

This is my JSON object

{
    "driver": [
        {
            "id": 1,
            "name": "Bob",
            "age": "34",
            "car": [
                {
                    "make": "BMW",
                    "model": "3.20",
                    "colour": "Silver",
                    "mileage": [
                        {
                            "total": "350523",
                            "year": [
                                {
                                    "2011": "3535",
                                    "2012": "7852",
                                    "2013": "8045"
                                }
                            ],
                            "month": [
                                {
                                    "december": "966",
                                    "november": "546",
                                    "october": "7657"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "id": 2,
            "name": "Rob",
            "age": "22",
            "car": [
                {
                    "make": "Peugeot",
                    "model": "306",
                    "colour": "Blue",
                    "mileage": [
                        {
                            "total": "125635",
                            "year": [
                                {
                                    "2011": "3276",
                                    "2012": "2445",
                                    "2013": "4566"
                                }
                            ],
                            "month": [
                                {
                                    "december": "966",
                                    "november": "546",
                                    "october": "157"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "id": 3,
            "name": "June",
            "age": "19",
            "car": [
                {
                    "make": "Mazda",
                    "model": "3",
                    "colour": "Red",
                    "mileage": [
                        {
                            "total": "550523",
                            "year": [
                                {
                                    "2011": "8456",
                                    "2012": "9552",
                                    "2013": "10633"
                                }
                            ],
                            "month": [
                                {
                                    "december": "625",
                                    "november": "2656",
                                    "october": "1332"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}


I want to extract a car for a particular driver. I can get the drive based on this code

JavaScript
var selectDriver = _.filter(data.driver, {id: 3});


But having issues with getting the car for this driver, I get undefined properties. What am I missing, or doing wrong with my following code

JavaScript
var driverCar = _.chain(selectDriver)
           .flatten(selectDriver)
           .pluck(selectDriver, "car")
           .map(function(car){
               return {
                   make : make,
                   model: car.model,
                   colour : car.colour
               }
           })
           .value();


Thanks for the help!
Posted

Try
C#
var search = _.filter(_.keys(data), function (key) {
    var obj = data[key];
    return _.every(query, function (val, queryKey) {
        return ref(obj, queryKey) === val;
    });
});
 
Share this answer
 
Here's one way to do it. I got it most of the way with plain JS. Only used underscore's flatten method.

C#
function getCarFor(driverId) {
  var cars = data.driver.filter(function(driverObject) {
    return driverObject.id === driverId;
  })
  .map(function(driver){
    return driver.car.map(function(car){
      return {
         make: driver.car[0].make,
         model: driver.car[0].model,
         colour: driver.car[0].colour
      };
    });
  });
  return _.flatten(cars)[0];  // assuming you want the first car
}

var car1 = getCarFor(1)
console.log(car1);
 
Share this answer
 
Comments
Ru_Ru 26-Nov-14 4:20am    
Great, it works! Not entirely sure how it works, but I will figure it out.
But isn't it better to use plain JS or Underscore methods for better performance?

With this code I can also be able to get the mileage object from the json string?
elridge-dmello 26-Nov-14 11:59am    
"But isn't it better to use plain JS or Underscore methods for better performance?"
-- My _opinion_ on this is that its best to use functions that already exist on JS arrays such as (filter, map, reduce, etc) before depending on a library. I'm all for using libraries to do things that plain JS doesn't. You could probably also do the above solution without use of _.flatten method, perhaps with a reduce function.

"With this code I can also be able to get the mileage object from the json string?"
You would have to add to some code to the function that maps the driver.car array.

To better understand how to work with nested JSON, there's a good interactive tutorial here: http://jhusain.github.io/learnrx/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900