'how to filter a complex object in react

I have an object like the following.

example:[
{
    "Id":"100",
    "value":"Egor1",
    "children":{
        "pilot":[
            {
                "Properties":{
                    "Id":123,
                    "History":[
                        20191101,
                        20191112,
                        20191103
                    ]
                }
            }
        ]
    }
},
{
    "Id":"200",
    "value":"Egor2",
    "children":{
        "pilot":[
            {
                "Properties":{
                    "Id":234,
                    "History":[
                        20191001,
                        20191012,
                        20191003,
                        20190902
                    ]
                }
            }
        ]
    }
},
{
    "Id":"300",
    "value":"Egor3",
    "children":{
        "pilot":[
            {
                "Properties":{
                    "Id":456,
                    "History":[
                        20190901,
                        20190912,
                        20190903,
                        20191101
                    ]
                }
            }
        ]
    }
}

]

I have an input 20191101. Sometimes input can be 20191101,20191001. I need to filter the example if

children.pilot.properties.history[0]=== 20191101.

I tried the following:

const result = example.filter( task => task.children.pilot.properties.history.includes(getPbfValueByFilter(task.childen, input))
  );

getPbfValueByFilter method :

const getPbfValueByFilter = (allChilden, input) => {
const { features } = allChilden.pilot;
const test = input.toString().split(',');

if (isUndefined(features) || isEmpty(features)
|| isUndefined(features.properties.History)) {
return [];
}
 test.map((each) => {
 if (each === features.properties.History[0]) {
 console.log("here" + features.properties.History[0])
 return allChilden;
}

});
};

expected output :

[
{
    "Id": "100",
    "value": "Egor1",
    "children": {
        "pilot": [
            {
                "Properties": {
                    "Id": 123,
                    "History": [
                        20191101,
                        20191112,
                        20191103
                    ]
                }
            }
        ]
    }
}
]

I am getting the console part. But it is unable to fetch the result. I assume that 'includes' is not working as expected. What went wrong. Please advice. TIA.



Solution 1:[1]

According to the example object pilot is an array so the filter should be something like this:

const result = example.filter((task) =>
  task.children.pilot[0].properties.history.includes(
    getPbfValueByFilter(task.children, input)
  )
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1