我尝试过多嵌套过滤器我正确输出了输出,但原始数据也被过滤掉了。请检查我的代码我不知道我在做什么错误可以任何人帮助我如何解决问题。
有没有更好的方法在java脚本中进行过滤?我在React.js
中开发这个过滤器可能性是国家/地区名称,国家/地区元标记,cityIds。 cityIds - > name,meta tag,portIds,portIds - > name,meta tag
https://jsfiddle.net/25sn49v8/
const country = [
{
"_id":"59b3b419424d6751ceafe984",
"name":"india country",
"metaTags":[
{
"id":1,
"text":"india country"
},
{
"id":2,
"text":"india"
}
],
"cityIds":[
{
"_id":"59c38704850f915d9715b93a",
"name":"chennai city",
"portIds":[
{
"_id":"59dc73d7f8c3033054e34fca",
"name":"chennai port",
},
{
"_id":"59df23af5a4fcc1d91201894",
"name":"Port",
"metaTags":[
{
"text":"Port",
"id":1
}
],
}
]
},
{
"_id":"59c4a9c0841f767b0ba80652",
"name":"Nungambakkam",
"metaTags":[
{
"id":1,
"text":"Nungambakkam"
}
],
"portIds":[
{
"_id":"59c4ec17841f767b0ba806a3",
"name":"mumbai city",
"metaTags":[
{
"id":1,
"text":"Mu"
}
],
}
]
}
]
},
{
"_id":"59b3b419424d6751ceafe984",
"name":"australia country",
"metaTags":[
{
"id":1,
"text":"australia country"
},
{
"id":2,
"text":"australia"
}
],
"cityIds":[
{
"_id":"59c38704850f915d9715b93a",
"name":"Sydney city",
"metaTags":[
{
"text":"Sydney",
"id":1
},
{
"text":"city",
"id":1
}
],
"portIds":[
{
"_id":"59dc73d7f8c3033054e34fca",
"name":"Sydney port",
"metaTags":[
{
"text":"Sydney port",
"id":1
}
],
},
{
"_id":"59df23af5a4fcc1d91201894",
"name":"Sydney two",
"metaTags":[
{
"text":"Sydney",
"id":1
}
],
}
],
},
{
"_id":"59c4a9c0841f767b0ba80652",
"name":"perth",
"metaTags":[
{
"id":1,
"text":"perth"
}
],
"portIds":[
{
"_id":"59c4ec17841f767b0ba806a3",
"name":"perth port",
"metaTags":[
{
"id":1,
"text":"perth xxx"
}
],
}
]
}
]
}
]
const filterTree = (filter, list) => {
return _.filter(list, (item) => {
//console.log(item,"AAAA")
if (_.includes(_.toLower(item.name), _.toLower(filter))) {
//console.log(item,"11111")
return true;
}
else if(_.findIndex( item.metaTags, function(o) { return _.includes(_.toLower(o.text), _.toLower(filter))}) >=0 ){
return true;
}
else if (item.cityIds) {
//console.log(item.cityIds,"22222")
item.cityIds = filterTree(filter, item.cityIds);
return !_.isEmpty(item.cityIds);
}
else if (item.portIds) {
//console.log(item,"33333",!_.isEmpty(item.portIds))
item.portIds = filterTree(filter, item.portIds);
return !_.isEmpty(item.portIds);
}
});
};
console.log(filterTree('perth xxx', country),"output");
console.log( country);