I've been struggling with removing an entire array if it contains an object with a certain value within. I've searched high and low, but haven't been able to find a solution that fits my specific problem.
In my data structure, I have arrays of objects which are wrapped by another array. It looks something like this:
"products": [
[
{
"product.name": "A",
"remark.name": "Good"
},
{
"product.name": "B",
"remark.name": "Good"
}
],
[
{
"product.name": "A",
"remark.name": "Bad"
},
{
"product.name": "B",
"remark.name": "Good"
}
]
]
Desired Outcome
I want to exclude any array that contains an object with at least remark.name === Bad
. The final result should look like this.
"products": [
[
{
"product.name": "A",
"remark.name": "Good"
},
{
"product.name": "B",
"remark.name": "Good"
}
]
]
Attempted Solution
I tried the following code snippet:
let result = [];
products.map((product) => {
var res = _.remove(product, function (n) {
return n["remark.name"] === "Fail";
});
result.push(res);
});
This produced the following result:
"products": [
[
{
"product.name": "A",
"remark.name": "Good"
},
{
"product.name": "B",
"remark.name": "Good"
}
],
[
{
"product.name": "B",
"remark.name": "Good"
}
]
]