Having difficulty extracting the nested car values using JavaScript (lodash). Take a look at the JSON data below:
{
"cars":[
{
"nestedCars":[
{
"car":"Truck",
"color":"Red",
"type":"Honda"
}
]
},
{
"nestedCars":[
{
"car":"Sedan",
"color":"Blue",
"type":"Ford"
}
]
}
]
}
The JSON response is retrieving the data correctly.
this.carLevels = response.data.cars;
The code snippet below is returning all data when only expecting two cars (Truck and Sedan).
carData() {
result = _.filter(this.carLevels, "nestedCars[0].car")
}
Tried using nested functions as well but with no success.
result = this.carLevels.filter(function (a) {
return a.nestedCars.some(function (b) {
return b.car;
});
});
Not sure what I'm doing incorrectly here.
Essentially, trying to retrieve all car items from the JSON data.
Expected output:
car:"Truck"
car:"Sedan"