Is array reduce the best option for this or any other better options like using lodash
For eg:- The array that needs to be reduced
let A1 = [
{
"id": 1,
"agency": "Sel A.",
"analysis": [
{"quarter": "Q4"," comment": "Test Data 1"},
{"quarter": "Q3", "comment": "Test Data 2"},
{"quarter": "Q2", "comment": "Test Data 3"}]
},
{
"id": 2,
"agency": "Sel B.",
"analysis": [
{"quarter": "Q1", "comment": "Test Data 4"},
{"quarter": "Q3", "comment": "Test Data 5"},
{"quarter": "Q4", "comment": "Test Data 6"}]
},
{
"id": 3,
"agency": "Sel C.",
"analysis": [
{"quarter": "Q2", "comment": "Test Data 7"},
{"quarter": "Q3", "comment": "Test Data 8"},
{"quarter": "Q4", "comment": "Test Data 9"}]
},
{
"id": 4,
"agency": "Sel D.",
"analysis": [
{"quarter": "Q1", "comment": "Test Data 10"},
{"quarter": "Q4", "comment": ""},
{"quarter": "Q3", "comment": "Test Data 12"}]
}
];
The array with which we get the filter
let A2 = ['Q3', Q4];
How to reduce array like below where it only filter 'Q3' and 'Q4' and also check if comment is not empty.
let res = [
{
"id": 1,
"agency": "Sel A.",
"analysis": [
{"quarter": "Q4"," comment": "Test Data 1"},
{"quarter": "Q3", "comment": "Test Data 2"}],
},
{
"id": 2,
"agency": "Sel B.",
"analysis": [,
{"quarter": "Q3", "comment": "Test Data 5"},
{"quarter": "Q4", "comment": "Test Data 6"}]
},
{
"id": 3,
"agency": "Sel C.",
"analysis": [
{"quarter": "Q3", "comment": "Test Data 8"},
{"quarter": "Q4", "comment": "Test Data 9"}]
},
{
"id": 4,
"agency": "Sel D.",
"analysis": [
{"quarter": "Q3", "comment": "Test Data 12"}]
}
]