I have some data like this, it’s more complex than this but this is the basic structure.
{
"AUDI": {
"Id": "3",
"Name": "AUDI",
"Models": {
"A1": [
{
"Model": "A1",
"TransmissionName": "Manual",
"FuelTypeName": "Petrol"
},
{
"Model": "A1",
"TransmissionName": "Manual",
"FuelTypeName": "Petrol"
}
],
"A3": [
{
"Model": "A3",
"TransmissionName": "Manual",
"FuelTypeName": "Petrol"
}
]
}
},
"BMW": {
"Id": "6",
"Name": "BMW",
"Models": {
"1 SERIES": [
{
"Model": "1 SERIES",
"TransmissionName": "Manual",
"FuelTypeName": "Diesel"
},
{
"Model": "1 SERIES",
"TransmissionName": "Manual",
"FuelTypeName": "Petrol"
}
]
}
}
}
I’m trying to filter the object down using a number of possible variables.
This is the code I’ve got so far:
const newObj = _
.chain(o).pickBy((o) => {
if (makeId != null) {
return o.Id == makeId // Corresponding to the Id value
} else {
return o
}
}).forEach((o) => {
if (model != null) {
return o.Models.Model == model // Corresponding to the Model value within the Models array
} else {
return o
}
}).value()
The first bit works (IE the Make selection), and I know I’m doing something fundamentally wrong on the .forEach part, but it just keeps returning the whole of the Make object, IE all the AUDIs or all the BWMs.
I would like to then be able to go on and filter by Model EG. is it petrol, is it a manual etc.
Would really appreciate some pointers please. Been trying to get this to work all day.
Thanks.