我有一个有效的json文件(我只放入其中的一部分)。我想搜索不区分大小写并查找为包含(假设json文件中有Red Carpet记录,如果用户搜索“ red”,它将显示匹配项的video_url(“ Red Carpet”的video_url)。我有一个限制,我可以做到)由于硬件原因,请使用ES6。请说明如何将以下json文件转换为javascript对象,并使用jquery或javascript进行包含不区分大小写的搜索。为此,我可以在项目中添加lodash或任何其他javascript库。 / p>
[{
"video_url": "http://63.237.48.3/ipnx/media/movies/Zane_Ziadi_HQ.mp4",
"title": "Zane Ziadi"
},
{
"video_url": "http://63.237.48.3/ipnx/media/movies/DarBastAzadiHQ.mp4",
"title": "Darbast Azadi"
},
{
"video_url": "http://63.237.48.3/ipnx/media/movies/Cheghadr_Vaght_Dari_HQ.mp4",
"title": "Cheghadr Vaght Dari"
},
{
"video_url": "http://63.237.48.3/ipnx/media/movies/Mashaal_HQ.mp4",
"title": "Mashaal"
},
{
"video_url": "http://63.237.48.3/ipnx/media/movies/Red_Carpet_HQ.mp4",
"title": "Red Carpet"
}
]
以下代码无效:
document.getElementById('input').addEventListener('change', function() {
var name = document.getElementById('input').value.toLowerCase();
var result = _.find(movies, {'title': name});
if (!result){
console.log('Nothing found');
}else{
console.log('Go to ' + result.video_url);
}
});
Edit2:update2
通过Akrion评论修复并解决,非常感谢他
Edit3:update3
Akrion答案似乎是,在搜索字符“ a”时,它不会返回以A为字符的所有标题,请修改并更正我尝试运行ES5部分答案的代码,并且仅返回zane ziadi项目,而不是“ Cheghadr Vaght Dari”
Edit4:update4 正如Akrion在他的评论中建议的那样,我可以使用过滤器而不是查找来匹配所有搜索。我将他对这个主题的回答永远标记为
答案 0 :(得分:2)
您的代码存在的问题是,您要求lodash
比较title
的确切内容,并且更重要的是,在区分大小写的情况下进行比较,因为{title: name}
会触发lodash中的属性匹配它只是将标题的内容与变量的内容进行比较。
您可以通过lodash
方法创建搜索功能,而无需这样的ES6:
const data = [{ "video_url": "http://63.237.48.3/ipnx/media/movies/Zane_Ziadi_HQ.mp4", "title": "Zane Ziadi" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/DarBastAzadiHQ.mp4", "title": "Darbast Azadi" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Cheghadr_Vaght_Dari_HQ.mp4", "title": "Cheghadr Vaght Dari" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Mashaal_HQ.mp4", "title": "Mashaal" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Red_Carpet_HQ.mp4", "title": "Red Carpet" } ]
const search = function(data, term) {
return _.find(data, function(x) {
return _.includes(_.toLower(x.title), _.toLower(term))})
}
console.log(search(data, 'azadi'))
console.log(search(data, 'Red'))
console.log(search(data, 'zan'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
使用find
,includes
和toLower
lodash方法。
您的代码将如下所示:
function search(data, term) {
return _.find(data, function(x) {
return _.includes(_.toLower(x.title), _.toLower(term))})
}
document.getElementById('input').addEventListener('change', function() {
var name = document.getElementById('input').value;
var result = search(movies, name); // <-- change to use the new search fn
if (result) {
console.log('Nothing found');
} else {
console.log('Go to ' + result.video_url);
}
});
使用 ES5 ,您还可以通过以下方式进行操作:
const data = [{ "video_url": "http://63.237.48.3/ipnx/media/movies/Zane_Ziadi_HQ.mp4", "title": "Zane Ziadi" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/DarBastAzadiHQ.mp4", "title": "Darbast Azadi" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Cheghadr_Vaght_Dari_HQ.mp4", "title": "Cheghadr Vaght Dari" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Mashaal_HQ.mp4", "title": "Mashaal" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Red_Carpet_HQ.mp4", "title": "Red Carpet" } ]
const search = function(data, term) {
return data.find(function(x) {
return x.title.toLowerCase().indexOf(term.toLowerCase()) >= 0 })
}
console.log(search(data, 'azadi'))
console.log(search(data, 'Red'))
console.log(search(data, 'zan'))
使用 ES6 ,它就是:
const data = [{ "video_url": "http://63.237.48.3/ipnx/media/movies/Zane_Ziadi_HQ.mp4", "title": "Zane Ziadi" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/DarBastAzadiHQ.mp4", "title": "Darbast Azadi" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Cheghadr_Vaght_Dari_HQ.mp4", "title": "Cheghadr Vaght Dari" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Mashaal_HQ.mp4", "title": "Mashaal" }, { "video_url": "http://63.237.48.3/ipnx/media/movies/Red_Carpet_HQ.mp4", "title": "Red Carpet" } ]
const search = (data, term) =>
data.find(({title}) => title.toLowerCase().includes(term.toLowerCase()))
console.log(search(data, 'azadi'))
console.log(search(data, 'Red'))
console.log(search(data, 'zan'))
请注意,这不是一种超级性能的方法,如果您要以这种方式搜索成百上千的文档,效果将会不佳。您需要研究其他方法(二进制搜索等)来解决此问题。