I have a list which can have the following kinds of values:
[
{ "a": "ABC", "b": "DEF", "c": "IAABS", "d": "XYZ" },
{ "a": "ABC", "b": "DEF", "c": "*ALL", "d": "*ALL" },
{ "a": "LMN", "b": "BIFPSR", "c": "*ALL", "d": "FGH" },
{ "a": "YUI", "b": "BIFPSR", "c": "TEST", "d": "XYZ" },
{ "a": "LMN", "b": "BIFPSR", "c": "TEST", "d": "FGH" },
]
Below are my test cases which I want to pass:
Case 1: If more than 1 object conatins same libraryName
& sourceFile
as a combination then check if any of those objects contain memberName
& memberType
as *ALL or not. If yes, then remove the other objects.
Case 2: If more than 1 object conatins same libraryName
& sourceFile
as a combination then check if any of those objects have memberName
as *ALL and memberType
as any value. If yes, then based on the memberType
‘s value check which objects have same value for the same libraryName
& sourceFile
as a combination then those objects need to be removed.
Case 3: If above cases don’t match/list is blank as it is it should output.
Expected Output from the above cases:
[
{ "a": "ABC", "b": "DEF", "c": "*ALL", "d": "*ALL" },
{ "a": "LMN", "b": "BIFPSR", "c": "*ALL", "d": "FGH" },
{ "a": "YUI", "b": "BIFPSR", "c": "TEST", "d": "XYZ" }
]
For the Case 1, I tried something like this below but it is printing the original array only –
function abc(arr) {
const seen = new Set(); // Set to store unique combination
return arr.reduce((acc, obj) => {
const key = `${obj.a}-${obj.b}`;
if (!seen.has(key)) {
seen.add(key);
acc.push(obj); // Adding unique elements or objects with *ALL
} else if (obj.c === '*ALL') {
acc.push(obj);
}
return acc;
}, []);
}