I have a JSON file containing data that requires filtering.
Here is an example structure of the JSON:
[{A:"data", C:"flightData", D:"FlightData"},
{B:"data", C:"flightData", D:"FlightData"},
{A:"data", C:"flightData", D:"FlightData"},
{B:"data", C:"flightData"},
{A:"data", C:"flightData", D:"FlightData"},
{B:"data", D:"FlightData"}]
I am currently utilizing the 'group by' method.
function groupBy(list, keyGetter) {
const map = new Map();
list.forEach((item) => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
}
Initial grouping is done based on either A or B into Arrays
A is [[C],[D]]
and B is[[C],[D]]
In cases where the data does not contain either C or D, the group needs to be removed as the data is irrelevant.
for (let value of s1) {
// A or B
const routeGrouped = groupBy(PriceArrayFinal, route =>
route.FinalArrival);
for (let origin of s3) {
// C or D
const originGrouped = groupBy(routeGrouped.get(value), route =>
route.FirstDepartCityName);
Is it possible to delete the entire group such as A: [ [C], [] ]
if either C or D is missing? If there is an empty array, the A or B group needs to be removed?
EDIT: Desired Output: All groups of A or B must include both C and D
[{A:"data", C:"flightData", D:"FlightData"},
{B:"data", C:"flightData", D:"FlightData"},
{A:"data", C:"flightData", D:"FlightData"},
{A:"data", C:"flightData", D:"FlightData"}]
[ [ [Array], [Array] ], [ [Array], [Array] ], [ [Array], [Array] ], [ [Array], [Array] ] ]