我需要使用lo-dash过滤器过滤国家/地区详细信息。尽管过滤器工作得很好,但它取代了我不想要的原始国家阵列。
我在React.js中开发它。这是一个过滤器,过滤如下:
国 国家/地区名称,国家元标记,cityIds。 cityIds - > name,meta tag,portIds,portIds - > name,meta tag
我附上了小提琴。
https://jsfiddle.net/6tttaq6c/2/
const country = [{
"_id": "59b3b419424d6751ceafe984",
"iso": "IN",
"__v": 0,
"status": 1,
"cityIds": [{
"_id": "59c38704850f915d9715b93a",
"countryId": "59b3b419424d6751ceafe984",
"__v": 3,
"status": 1,
"portIds": [{
"_id": "59dc73d7f8c3033054e34fca",
"cityId": "59c38704850f915d9715b93a",
"longLat": [80.2425, 13.0595],
"__v": 0,
"status": 1,
"relatedPods": [{
"_id": "59bf7b7cdd1e5f1af51fd4f0",
"cityId": null,
"isDdpService": 1,
"longLat": [72.8777, 19.076],
"__v": 0,
"status": 1,
"relatedPods": ["59c4ab42841f767b0ba8065b", "59bf7b7cdd1e5f1af51fd4f0"],
"isDoorService": 1,
"metaTags": [{
"id": 1,
"text": "Mumbai"
}],
"name": "Mumbai China Port"
}, {
"_id": "59cd89b74f47ae1b04600fd1",
"cityId": {
"_id": "59cd88994f47ae1b04600fce",
"countryId": null,
"__v": 0,
"status": 1,
"portIds": ["59cd89b74f47ae1b04600fd1"],
"metaTags": [],
"name": "TestC1299"
},
"longLat": [20, 10],
"__v": 0,
"status": 1,
"relatedPods": ["59cd8a094f47ae1b04600fd2"],
"isDoorService": 1,
"metaTags": [],
"name": "TestP1299"
}, {
"_id": "59c4ebe5841f767b0ba806a2",
"cityId": {
"_id": "59c38704850f915d9715b93a",
"countryId": {
"_id": "59b3b419424d6751ceafe984",
"iso": "IN",
"__v": 0,
"status": 1,
"cityIds": ["59b66e27424d6751ceafe98e", "59ba41aa424d6751ceafe99c", "59c3877d850f915d9715b93b", "59c3877f850f915d9715b93c", "59c38784850f915d9715b93e", "59c3878a850f915d9715b940", "59c38798850f915d9715b942", "59c38704850f915d9715b93a", "59c4a9c0841f767b0ba80652", "59c4a9d5841f767b0ba80653", "59c4aa1d841f767b0ba80654", "59c4d9a7841f767b0ba8067b", "59c4d9f6841f767b0ba8067c", "59c4df53841f767b0ba80685", "59defc134417e706e0f9fe82", "59defd0b52068d07d150ca8a", "59df4fc6ae861956765ac72b", "59e457fa27a94c49df6dc16b", "59e4656127fce563f6556f1c", "59e465ef27fce563f6556f1e", "59e469f2381f686991238c16", "59e46a56bf9d006a99e568a1", "59e46adc231f5a6c2252833e", "59e46b2a231f5a6c22528340", "59e46c2892fd5170ac3bec7f", "59e4586e4a053c4b10ae5724", "59f2c23fbeccec3b4cd51a91"],
"ddpServiceId": ["59ed5cb229ceca7cb1d3701a"],
"isDdpService": 0,
"usdExchangeRate": 0.015445207408516145,
"currency": "INR",
"metaTags": [{
"id": 1,
"text": "IN"
}, {
"id": 2,
"text": "india"
}],
"name": "India"
},
"__v": 3,
"status": 1,
"portIds": ["59c4a64d841f767b0ba80651", "59c4e31a841f767b0ba8068f", "59c4ebe5841f767b0ba806a2", "59dc634e196d075fa7e2a45c", "59dc73d7f8c3033054e34fca", "59dc76cbcb4f3c3d00080f82", "59df14ec5a4fcc1d91201831", "59df1fcd5a4fcc1d91201866", "59df23af5a4fcc1d91201894", "59df4d154609214547e85867", "59e1e824749a8b07ac206249", "59e8a512cbfaf6752210cbce", "59e8b000cbfaf6752210cbd3", "59e8b025cbfaf6752210cbd4", "59f1cfe8b558db21a88a36f4"],
"metaTags": [{
"text": "hope",
"id": 1
}],
"name": "chennai"
},
"longLat": [80.237617, 13.067439],
"__v": 1,
"status": 1,
"relatedPods": ["59c4ec17841f767b0ba806a3"],
"isDoorService": 1,
"metaTags": [{
"id": 1,
"text": "TP"
}, {
"text": "beach",
"id": 2
}],
"name": "beach station"
}],
"isDoorService": 1,
"metaTags": [],
"name": "chennai port"
}, {
"_id": "59df23af5a4fcc1d91201894",
"cityId": "59c38704850f915d9715b93a",
"longLat": [80.2707, 13.0827],
"__v": 0,
"status": 1,
"relatedPods": [{
"_id": "59df23e75a4fcc1d91201895",
"cityId": {
"_id": "59c4a9d5841f767b0ba80653",
"countryId": {
"_id": "59b3b419424d6751ceafe984",
"iso": "IN",
"__v": 0,
"status": 1,
"cityIds": ["59b66e27424d6751ceafe98e", "59ba41aa424d6751ceafe99c", "59c3877d850f915d9715b93b", "59c3877f850f915d9715b93c", "59c38784850f915d9715b93e", "59c3878a850f915d9715b940", "59c38798850f915d9715b942", "59c38704850f915d9715b93a", "59c4a9c0841f767b0ba80652", "59c4a9d5841f767b0ba80653", "59c4aa1d841f767b0ba80654", "59c4d9a7841f767b0ba8067b", "59c4d9f6841f767b0ba8067c", "59c4df53841f767b0ba80685", "59defc134417e706e0f9fe82", "59defd0b52068d07d150ca8a", "59df4fc6ae861956765ac72b", "59e457fa27a94c49df6dc16b", "59e4656127fce563f6556f1c", "59e465ef27fce563f6556f1e", "59e469f2381f686991238c16", "59e46a56bf9d006a99e568a1", "59e46adc231f5a6c2252833e", "59e46b2a231f5a6c22528340", "59e46c2892fd5170ac3bec7f", "59e4586e4a053c4b10ae5724", "59f2c23fbeccec3b4cd51a91"],
"ddpServiceId": ["59ed5cb229ceca7cb1d3701a"],
"isDdpService": 0,
"usdExchangeRate": 0.015445207408516145,
"currency": "INR",
"metaTags": [{
"id": 1,
"text": "IN"
}, {
"id": 2,
"text": "india"
}],
"name": "India"
},
"__v": 0,
"status": 1,
"portIds": ["59c4ab42841f767b0ba8065b", "59c4ab65841f767b0ba8065c", "59dddbb3a8dd2014c4ae04d9", "59df1ff55a4fcc1d91201867", "59df23e75a4fcc1d91201895", "59e8a371cbfaf6752210cbcb", "59e8a401cbfaf6752210cbcc"],
"metaTags": [{
"id": 1,
"text": "bangulure"
}],
"name": "bangulure"
},
"longLat": [77.580643, 12.972442],
"__v": 0,
"status": 1,
"relatedPods": [],
"isDoorService": 0,
"metaTags": [],
"name": "Port(Dest)"
}, {
"_id": "59df4d314609214547e85868",
"cityId": {
"_id": "59ccb67a4f47ae1b04600fc9",
"countryId": {
"_id": "59d782ec669d607c2add51f7",
"__v": 0,
"iso": "LK",
"status": 1,
"cityIds": ["59ccb67a4f47ae1b04600fc9"],
"ddpServiceId": null,
"isDdpService": 0,
"usdExchangeRate": 0.006507451116124828,
"currency": "LKR",
"metaTags": [],
"name": "Sri Lanka"
},
"__v": 1,
"status": 1,
"portIds": ["59df4d314609214547e85868", "59df1cdd5a4fcc1d91201852", "59f1bb2619e254063935ac44"],
"metaTags": [{
"text": "zz",
"id": 1
}, {
"text": "sricol",
"id": 2
}],
"name": "colomboo"
},
"longLat": [72.8777, 19.076],
"__v": 0,
"status": 1,
"relatedPods": [],
"isDoorService": 0,
"metaTags": [],
"name": "dest"
}],
"isDoorService": 0,
"metaTags": [],
"name": "Port(source)"
}],
"metaTags": [{
"text": "hope",
"id": 1
}],
"name": "chennai"
}, {
"_id": "59c4a9c0841f767b0ba80652",
"countryId": "59b3b419424d6751ceafe984",
"__v": 0,
"status": 1,
"portIds": [{
"_id": "59c4ec17841f767b0ba806a3",
"cityId": "59c4a9c0841f767b0ba80652",
"longLat": [80.2425, 13.0595],
"__v": 1,
"status": 1,
"relatedPods": [{
"_id": "59c4ebe5841f767b0ba806a2",
"cityId": {
"_id": "59c38704850f915d9715b93a",
"countryId": {
"_id": "59b3b419424d6751ceafe984",
"iso": "IN",
"__v": 0,
"status": 1,
"cityIds": ["59b66e27424d6751ceafe98e", "59ba41aa424d6751ceafe99c", "59c3877d850f915d9715b93b", "59c3877f850f915d9715b93c", "59c38784850f915d9715b93e", "59c3878a850f915d9715b940", "59c38798850f915d9715b942", "59c38704850f915d9715b93a", "59c4a9c0841f767b0ba80652", "59c4a9d5841f767b0ba80653", "59c4aa1d841f767b0ba80654", "59c4d9a7841f767b0ba8067b", "59c4d9f6841f767b0ba8067c", "59c4df53841f767b0ba80685", "59defc134417e706e0f9fe82", "59defd0b52068d07d150ca8a", "59df4fc6ae861956765ac72b", "59e457fa27a94c49df6dc16b", "59e4656127fce563f6556f1c", "59e465ef27fce563f6556f1e", "59e469f2381f686991238c16", "59e46a56bf9d006a99e568a1", "59e46adc231f5a6c2252833e", "59e46b2a231f5a6c22528340", "59e46c2892fd5170ac3bec7f", "59e4586e4a053c4b10ae5724", "59f2c23fbeccec3b4cd51a91"],
"ddpServiceId": ["59ed5cb229ceca7cb1d3701a"],
"isDdpService": 0,
"usdExchangeRate": 0.015445207408516145,
"currency": "INR",
"metaTags": [{
"id": 1,
"text": "IN"
}, {
"id": 2,
"text": "india"
}],
"name": "India"
},
"__v": 3,
"status": 1,
"portIds": ["59c4a64d841f767b0ba80651", "59c4e31a841f767b0ba8068f", "59c4ebe5841f767b0ba806a2", "59dc634e196d075fa7e2a45c", "59dc73d7f8c3033054e34fca", "59dc76cbcb4f3c3d00080f82", "59df14ec5a4fcc1d91201831", "59df1fcd5a4fcc1d91201866", "59df23af5a4fcc1d91201894", "59df4d154609214547e85867", "59e1e824749a8b07ac206249", "59e8a512cbfaf6752210cbce", "59e8b000cbfaf6752210cbd3", "59e8b025cbfaf6752210cbd4", "59f1cfe8b558db21a88a36f4"],
"metaTags": [{
"text": "hope",
"id": 1
}],
"name": "chennai"
},
"longLat": [80.237617, 13.067439],
"__v": 1,
"status": 1,
"relatedPods": ["59c4ec17841f767b0ba806a3"],
"isDoorService": 1,
"metaTags": [{
"id": 1,
"text": "TP"
}, {
"text": "beach",
"id": 2
}],
"name": "beach station"
}],
"isDoorService": 1,
"metaTags": [{
"id": 1,
"text": "Mu"
}],
"name": "Nungambakkam"
}, {
"_id": "59df13b35a4fcc1d91201829",
"cityId": "59c4a9c0841f767b0ba80652",
"longLat": [21.34, 17],
"__v": 0,
"status": 1,
"relatedPods": [{
"_id": "59dc73d7f8c3033054e34fca",
"cityId": {
"_id": "59c38704850f915d9715b93a",
"countryId": {
"_id": "59b3b419424d6751ceafe984",
"iso": "IN",
"__v": 0,
"status": 1,
"cityIds": ["59b66e27424d6751ceafe98e", "59ba41aa424d6751ceafe99c", "59c3877d850f915d9715b93b", "59c3877f850f915d9715b93c", "59c38784850f915d9715b93e", "59c3878a850f915d9715b940", "59c38798850f915d9715b942", "59c38704850f915d9715b93a", "59c4a9c0841f767b0ba80652", "59c4a9d5841f767b0ba80653", "59c4aa1d841f767b0ba80654", "59c4d9a7841f767b0ba8067b", "59c4d9f6841f767b0ba8067c", "59c4df53841f767b0ba80685", "59defc134417e706e0f9fe82", "59defd0b52068d07d150ca8a", "59df4fc6ae861956765ac72b", "59e457fa27a94c49df6dc16b", "59e4656127fce563f6556f1c", "59e465ef27fce563f6556f1e", "59e469f2381f686991238c16", "59e46a56bf9d006a99e568a1", "59e46adc231f5a6c2252833e", "59e46b2a231f5a6c22528340", "59e46c2892fd5170ac3bec7f", "59e4586e4a053c4b10ae5724", "59f2c23fbeccec3b4cd51a91"],
"ddpServiceId": ["59ed5cb229ceca7cb1d3701a"],
"isDdpService": 0,
"usdExchangeRate": 0.015445207408516145,
"currency": "INR",
"metaTags": [{
"id": 1,
"text": "IN"
}, {
"id": 2,
"text": "india"
}],
"name": "India"
},
"__v": 3,
"status": 1,
"portIds": ["59c4a64d841f767b0ba80651", "59c4e31a841f767b0ba8068f", "59c4ebe5841f767b0ba806a2", "59dc634e196d075fa7e2a45c", "59dc73d7f8c3033054e34fca", "59dc76cbcb4f3c3d00080f82", "59df14ec5a4fcc1d91201831", "59df1fcd5a4fcc1d91201866", "59df23af5a4fcc1d91201894", "59df4d154609214547e85867", "59e1e824749a8b07ac206249", "59e8a512cbfaf6752210cbce", "59e8b000cbfaf6752210cbd3", "59e8b025cbfaf6752210cbd4", "59f1cfe8b558db21a88a36f4"],
"metaTags": [{
"text": "hope",
"id": 1
}],
"name": "chennai"
},
"longLat": [80.2425, 13.0595],
"__v": 0,
"status": 1,
"relatedPods": ["59bf7b7cdd1e5f1af51fd4f0", "59dc76cbcb4f3c3d00080f82", "59cd89b74f47ae1b04600fd1", "59c4ebe5841f767b0ba806a2"],
"isDoorService": 1,
"metaTags": [],
"name": "chennai port"
}],
"isDoorService": 0,
"metaTags": [],
"name": "mumbai"
}],
"metaTags": [{
"id": 1,
"text": "mumbai"
}],
"name": "mumbai"
}, {
"_id": "59c4a9d5841f767b0ba80653",
"countryId": "59b3b419424d6751ceafe984",
"__v": 0,
"status": 1,
"portIds": [],
"metaTags": [{
"id": 1,
"text": "bangulure"
}],
"name": "bangulure"
}, {
"_id": "59df4fc6ae861956765ac72b",
"countryId": "59b3b419424d6751ceafe984",
"__v": 0,
"status": 1,
"portIds": [],
"metaTags": [{
"id": 1,
"text": "chennai"
}],
"name": "chennai"
}, {
"_id": "59e457fa27a94c49df6dc16b",
"countryId": "59b3addf424d6751ceafe983",
"__v": 3,
"status": 1,
"portIds": [],
"metaTags": [{
"text": "newbei",
"id": 1
}, {
"text": "jing",
"id": 2
}],
"name": "newbeijing"
}, {
"_id": "59f2c23fbeccec3b4cd51a91",
"countryId": "59b3b419424d6751ceafe984",
"__v": 0,
"status": 1,
"portIds": [{
"_id": "59f2c5f1beccec3b4cd51a99",
"cityId": "59f2c23fbeccec3b4cd51a91",
"longLat": [80.2707, 13.0827],
"__v": 0,
"status": 1,
"relatedPods": [{
"_id": "59f2c5f1beccec3b4cd51a9a",
"cityId": {
"_id": "59c4a9c0841f767b0ba80652",
"countryId": {
"_id": "59b3b419424d6751ceafe984",
"iso": "IN",
"__v": 0,
"status": 1,
"cityIds": ["59b66e27424d6751ceafe98e", "59ba41aa424d6751ceafe99c", "59c3877d850f915d9715b93b", "59c3877f850f915d9715b93c", "59c38784850f915d9715b93e", "59c3878a850f915d9715b940", "59c38798850f915d9715b942", "59c38704850f915d9715b93a", "59c4a9c0841f767b0ba80652", "59c4a9d5841f767b0ba80653", "59c4aa1d841f767b0ba80654", "59c4d9a7841f767b0ba8067b", "59c4d9f6841f767b0ba8067c", "59c4df53841f767b0ba80685", "59defc134417e706e0f9fe82", "59defd0b52068d07d150ca8a", "59df4fc6ae861956765ac72b", "59e457fa27a94c49df6dc16b", "59e4656127fce563f6556f1c", "59e465ef27fce563f6556f1e", "59e469f2381f686991238c16", "59e46a56bf9d006a99e568a1", "59e46adc231f5a6c2252833e", "59e46b2a231f5a6c22528340", "59e46c2892fd5170ac3bec7f", "59e4586e4a053c4b10ae5724", "59f2c23fbeccec3b4cd51a91"],
"ddpServiceId": ["59ed5cb229ceca7cb1d3701a"],
"isDdpService": 0,
"usdExchangeRate": 0.015445207408516145,
"currency": "INR",
"metaTags": [{
"id": 1,
"text": "IN"
}, {
"id": 2,
"text": "india"
}],
"name": "India"
},
"__v": 0,
"status": 1,
"portIds": ["59c4aaf1841f767b0ba80659", "59c4ab03841f767b0ba8065a", "59c4ec17841f767b0ba806a3", "59df13b35a4fcc1d91201829", "59e1e813749a8b07ac206248", "59e5adf14a0803445703af8d", "59e5b52684db48454bdf7253", "59e8af17cbfaf6752210cbcf", "59e8af22cbfaf6752210cbd1", "59e8b1dacbfaf6752210cbd5", "59e8b1eecbfaf6752210cbd7", "59e8b1f1cbfaf6752210cbd9", "59e8b1fbcbfaf6752210cbdb", "59e8b2fecbfaf6752210cbde", "59f1cc0de9f7c41d9af5dfb2", "59f2c2b9beccec3b4cd51a92", "59f2c5f1beccec3b4cd51a97", "59f2c5f1beccec3b4cd51a9a"],
"metaTags": [{
"id": 1,
"text": "mumbai"
}],
"name": "mumbai"
},
"longLat": [72.8777, 19.076],
"__v": 0,
"status": 1,
"relatedPods": [],
"isDoorService": 1,
"metaTags": [{
"id": 1,
"text": ""
}],
"name": "Mumbai "
}],
"isDoorService": 1,
"metaTags": [{
"id": 1,
"text": ""
}],
"name": "CHENNAI"
}],
"metaTags": [],
"name": "CHENNAI"
}],
"ddpServiceId": ["59ed5cb229ceca7cb1d3701a"],
"isDdpService": 0,
"usdExchangeRate": 0.015445207408516145,
"currency": "INR",
"metaTags": [{
"id": 1,
"text": "IN"
}, {
"id": 2,
"text": "india"
}],
"name": "India"
}, {
"_id": "59d782ec669d607c2add51f7",
"__v": 0,
"iso": "LK",
"status": 1,
"cityIds": [{
"_id": "59ccb67a4f47ae1b04600fc9",
"countryId": "59d782ec669d607c2add51f7",
"__v": 1,
"status": 1,
"portIds": [{
"_id": "59df1cdd5a4fcc1d91201852",
"cityId": "59ccb67a4f47ae1b04600fc9",
"longLat": [79.843561, 6.939517],
"__v": 0,
"status": 1,
"relatedPods": [{
"_id": "59df1d485a4fcc1d91201853",
"cityId": {
"_id": "59cd88a44f47ae1b04600fcf",
"countryId": null,
"__v": 0,
"status": 1,
"portIds": ["59cd8a094f47ae1b04600fd2", "59df1d485a4fcc1d91201853"],
"metaTags": [],
"name": "TestC299"
},
"longLat": [-158.00196, 21.479587],
"__v": 0,
"status": 1,
"relatedPods": [],
"isDoorService": 0,
"metaTags": [],
"name": "Hawaii"
}],
"isDoorService": 1,
"metaTags": [{
"id": 1,
"text": "col"
}],
"name": "colombo"
}],
"metaTags": [{
"text": "zz",
"id": 1
}, {
"text": "sricol",
"id": 2
}],
"name": "colomboo"
}],
"ddpServiceId": null,
"isDdpService": 0,
"usdExchangeRate": 0.006507451116124828,
"currency": "LKR",
"metaTags": [],
"name": "Sri Lanka"
}]
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('nungambakkam', country),"output");
console.log( country);