I have a an array of objects in this format:
const myArray = [
{
one: 'A',
two: 'AB'
},
{
one: undefined,
two: 'AB'
},
{
one: 'A',
two: 'BC'
},
{
one: undefined,
two: 'CC'
},
]
I want the output in this particular format where if one is undefined but two is present in another object then ignore that object. e.g., output:
const outputMyArray = [
{
one: 'A',
two: 'AB'
},
{
one: 'A',
two: 'BC'
},
{
one: undefined,
two: 'CC'
},
The object keys are dynamically generated hence cannot be hardcoded and each object can have same numbers of keys as others. This I have tried but did not work:
const objectValuesList = myArray.map((obj: any) => Object.values(obj))
const uniqueStrigifiedList = Array.from(new Set( objectValuesList.filter((obj: any) => !obj.includes(undefined)).map((obj: any) => JSON.stringify(obj)) ))
myArray?.filter((obj: any) => !uniqueStrigifiedList?.includes( String(Object.values(obj)) ))
This solution does not seem to be working as it considers all object keys as unique.