I have product structure as shown below:
product = {
"name":"MyXam",
"layers":[
{
"countries":[
{
"countryId":"1",
"countryName":"ABC"
},
{
"countryId":"2",
"countryName":"XYZ"
},
{
"countryId":"3",
"countryName":"PQR"
}
]
},
{
"countries":[
{
"countryId":"5",
"countryName":"LMN"
},
{
"countryId":"3",
"countryName":"PQR"
}
]
}
]
}
And selected countries:
selCountries = [
{
"countryId":"1"
},
{
"countryId":"3"
}
]
Now I want to filter the product in such a way that it should contain countries only that are in selCountries.
The final product should be:
{
"name":"MyXam",
"layers":[
{
"countries":[
{
"countryId":"1",
"countryName":"ABC"
},
{
"countryId":"3",
"countryName":"PQR"
}
]
},
{
"countries":[
{
"countryId":"3",
"countryName":"PQR"
}
]
}
]
}
I have tried the following using lodash but is not working:
_.filter(product.layers, _.flow(
_.property('countries'),
_.partialRight(_.some, selCountries)
));
As the product comes dynamically in my application. In some cases there is a possibility that some of the layers may have not countries. So the solution should handle this case also and should not break with undefined error.
Can any on help me, where I am going wrong?