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
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
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!