I'm having difficulty filtering an array within an object based on a condition (specifically, filter if value1 <> value2 and toggle is true)
myObject = {
value: 1,
capacitiesArray: [{value1: 10, value2: 10}, {value1: 10, value2: 20}]
}
the desired output should be
filteredObject = {
value: 1,
capacitiesArray: [{value1: 10, value2: 20}]
}
I've implemented a function called "filterObject", that takes "myObject" and "toggle" as parameters (if toggle is true then filter "capacitiesArray" else do not filter "capacitiesArray")
I attempted to achieve this by the following code, but it seems to not work as expected :(
I suspect there may be an issue with object mutation
function filterObject(myObject: MyObject, toggle: boolean): MyObject {
if(toggle) {
const rows = myObject.capacitiesArray;
const capacitiesArray = rows.filter((row) => row.value1 !== row.value2);
myObject.capacitiesArray = capacitiesArray;
return myObject;
}
return myObject;
}
Here's a link to unit test the function https://stackblitz.com/edit/github-test-run-draft-gczdsg?file=src%2Fapp%2Fapp.component.spec.ts
in app.component.spec.ts
the second test "filterRowOnCosCoeValue should filter operationalCapacities when toggle on" passes (which is correct, since the expected result is "expectedOperationalNoticeMock")
however, it should not pass when I provide "operationalNoticeMock" as the expected result (it still passes, giving a false positive result)
Any suggestions or help would be greatly appreciated! :) Thank you!
EDIT: additional details regarding the false positive test passing