如何在反应中过滤复杂的对象

雷亚

我有一个像下面这样的对象。

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
                    ]
                }
            }
        ]
    }
}

]

我有一个输入 20191101。有时输入可以是 20191101,20191001。我需要过滤这个例子,如果

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

我尝试了以下方法:

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

getPbfValueByFilter 方法:

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;
}

});
};

预期输出:

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

我正在获取控制台部分。但它无法获取结果。我认为“包含”没有按预期工作。什么地方出了错。请指教。TIA。

苦的

您可以使用somewith filter如果这就是您要找的,请告诉我:

var input=[20191101];
var 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 ] } } ] }}];

var result = example.filter(k=>k.children.pilot.some(d=>d.Properties.History.some(p=>input.includes(p))));

var result2 = example.filter(k=>k.children.pilot.some(d=>input.includes(d.Properties.History[0])));


console.log(result2);
//console.log(result);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章