I have an nested javascript object similar to this
const obj = {
topLevelProp1: 'some',
topLevelProp2: 'data',
topLevelPropArr: [
{
uid: 'd7ed3623-e431-4bfa-85b3-8da14fa399c3'
secLevelProp1: 123,
secLevelPropArr: [
{
uid: '15562309-22b7-4b11-a12e-b3374f0a7636',
thirdLevelProp1: 'a plain value',
thirdLevelProp2: {
a: 'I am',
b: 'a nested',
c: 'object',
}
},
{
uid: '9394f313-b7bf-45fa-b8f3-683832ff7aa2',
thirdLevelProp1: null,
thirdLevelProp2: {
a: 'I am also',
b: 'another nested',
c: 'object !!!',
}
}
]
},
{
uid: '04a487c6-9624-4b2e-8411-59a69c14f1e3'
secLevelProp1: 456,
secLevelPropArr: [
{
uid: '58d4a3fd-734c-4c22-a0e3-f669e6ec7af3',
thirdLevelProp1: 'another plain value',
],
}
],
};
The uid property values are unique for any given nested object (they are uuid v4 strings)
I also have an array of strings, describing a path to some nested property which looks like this:
const paths = [
'topLevelProp2',
'04a487c6-9624-4b2e-8411-59a69c14f1e3.secLevelProp1',
'd7ed3623-e431-4bfa-85b3-8da14fa399c3.9394f313-b7bf-45fa-b8f3-683832ff7aa2.thirdLevelProp2.a',
'd7ed3623-e431-4bfa-85b3-8da14fa399c3.9394f313-b7bf-45fa-b8f3-683832ff7aa2.thirdLevelProp2.c'
]
Instead of the keys of the array properties, the uid value of the contained object is used.
So the string'd7ed3623-e431-4bfa-85b3-8da14fa399c3.9394f313-b7bf-45fa-b8f3-683832ff7aa2.thirdLevelProp2.a'
represents actualy the path 'topLevelPropArr.0.secLevelPropArr.1.thirdLevelProp2.a'
in the more usual path-in-object notation.
I want to filter my object in a way, so it will only contain (all nested) properties included in the array of paths.
Using the above example, filtering obj by paths would result in:
{
topLevelProp2: 'data',
topLevelPropArr: [
{
secLevelProp1: 456,
secLevelPropArr: [
thirdLevelProp2: {
a: 'I am also',
c: 'object !!!',
}
]
},
{
secLevelProp1: 456,
}
]
}
Keeping the order of items in the arrays is not necessary.