how to case insentive contain search with lodash
I have a valid json file (i put only a limited part of that). i want to search incasesensitive and look as contain (suppose there is a record of Red Carpet in json file , if user search "red" it will show the matches's video_url (video_url of "Red Carpet). I have a limitation that i can't use ES6 because of hardware. please show how do i need to convert following json file to javascript object and use jquery or javascript to make a contain incasesensitive search. I can add lodash or any other javascript library to my project for this purpose.
[{
"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"
}
]
following code doesn't work :
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
fixed and solved by Akrion comment, thanks so much to him
Edit3: update3
it seemed by Akrion answer, when search a for character "a" it would not return all title which has A as character , please modify and correct the code i try to run ES5 part answer and it only return zane ziadi the first item but not "Cheghadr Vaght Dari"
Edit4: update4
as Akrion suggested in his comment i can use filter instead of find to match all searches. i mark his answer for this thread forever
javascript jquery arrays search lodash
add a comment |
I have a valid json file (i put only a limited part of that). i want to search incasesensitive and look as contain (suppose there is a record of Red Carpet in json file , if user search "red" it will show the matches's video_url (video_url of "Red Carpet). I have a limitation that i can't use ES6 because of hardware. please show how do i need to convert following json file to javascript object and use jquery or javascript to make a contain incasesensitive search. I can add lodash or any other javascript library to my project for this purpose.
[{
"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"
}
]
following code doesn't work :
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
fixed and solved by Akrion comment, thanks so much to him
Edit3: update3
it seemed by Akrion answer, when search a for character "a" it would not return all title which has A as character , please modify and correct the code i try to run ES5 part answer and it only return zane ziadi the first item but not "Cheghadr Vaght Dari"
Edit4: update4
as Akrion suggested in his comment i can use filter instead of find to match all searches. i mark his answer for this thread forever
javascript jquery arrays search lodash
@Akrion please see this related topic with different subject thanks so much : stackoverflow.com/questions/53312728/…
– Hamid
Nov 15 '18 at 5:13
add a comment |
I have a valid json file (i put only a limited part of that). i want to search incasesensitive and look as contain (suppose there is a record of Red Carpet in json file , if user search "red" it will show the matches's video_url (video_url of "Red Carpet). I have a limitation that i can't use ES6 because of hardware. please show how do i need to convert following json file to javascript object and use jquery or javascript to make a contain incasesensitive search. I can add lodash or any other javascript library to my project for this purpose.
[{
"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"
}
]
following code doesn't work :
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
fixed and solved by Akrion comment, thanks so much to him
Edit3: update3
it seemed by Akrion answer, when search a for character "a" it would not return all title which has A as character , please modify and correct the code i try to run ES5 part answer and it only return zane ziadi the first item but not "Cheghadr Vaght Dari"
Edit4: update4
as Akrion suggested in his comment i can use filter instead of find to match all searches. i mark his answer for this thread forever
javascript jquery arrays search lodash
I have a valid json file (i put only a limited part of that). i want to search incasesensitive and look as contain (suppose there is a record of Red Carpet in json file , if user search "red" it will show the matches's video_url (video_url of "Red Carpet). I have a limitation that i can't use ES6 because of hardware. please show how do i need to convert following json file to javascript object and use jquery or javascript to make a contain incasesensitive search. I can add lodash or any other javascript library to my project for this purpose.
[{
"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"
}
]
following code doesn't work :
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
fixed and solved by Akrion comment, thanks so much to him
Edit3: update3
it seemed by Akrion answer, when search a for character "a" it would not return all title which has A as character , please modify and correct the code i try to run ES5 part answer and it only return zane ziadi the first item but not "Cheghadr Vaght Dari"
Edit4: update4
as Akrion suggested in his comment i can use filter instead of find to match all searches. i mark his answer for this thread forever
javascript jquery arrays search lodash
javascript jquery arrays search lodash
edited Nov 15 '18 at 5:18
Hamid
asked Nov 14 '18 at 20:47
HamidHamid
206
206
@Akrion please see this related topic with different subject thanks so much : stackoverflow.com/questions/53312728/…
– Hamid
Nov 15 '18 at 5:13
add a comment |
@Akrion please see this related topic with different subject thanks so much : stackoverflow.com/questions/53312728/…
– Hamid
Nov 15 '18 at 5:13
@Akrion please see this related topic with different subject thanks so much : stackoverflow.com/questions/53312728/…
– Hamid
Nov 15 '18 at 5:13
@Akrion please see this related topic with different subject thanks so much : stackoverflow.com/questions/53312728/…
– Hamid
Nov 15 '18 at 5:13
add a comment |
1 Answer
1
active
oldest
votes
The issue with your code is that you are asking lodash to compare the exact contents of title and more importantly compare it while being case sensitive, since {title: name} triggers property match in lodash which simply compares the contents of title with the contents of the variable.
You can create a search function via lodash methods and no ES6 like this:
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>Using find, includes and toLower lodash methods.
Your code then would look like:
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);
}
});
With ES5 you can also do this via:
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'))With ES6 this would simply be:
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'))Please note that this is not a super performant approach and if you are going to search hundreds/thousands of documents this way it will not perform well. You would need to look into other ways (Binary search etc) to solve this.
Interesting they all work in the browser. What is the error for the lodash one? First suggestion.
– Akrion
Nov 14 '18 at 22:19
So you are telling me that if you add thesearchfunction from any of the top 2 code samples and in your code just change fromvar result = _.find(movies, {'title': name});tovar result = search(movies, name);it is not working and it gives an exception?
– Akrion
Nov 14 '18 at 22:25
I updated the 2nd paragraph. Hope this helps
– Akrion
Nov 14 '18 at 23:21
In your original post you had the collection named asmoviesso I left that.mvoiesshould be the name of the collection which you will be searching against.
– Akrion
Nov 14 '18 at 23:32
See comment. Search works just fine. Your logic is not correct.
– Akrion
Nov 14 '18 at 23:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308477%2fhow-to-case-insentive-contain-search-with-lodash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue with your code is that you are asking lodash to compare the exact contents of title and more importantly compare it while being case sensitive, since {title: name} triggers property match in lodash which simply compares the contents of title with the contents of the variable.
You can create a search function via lodash methods and no ES6 like this:
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>Using find, includes and toLower lodash methods.
Your code then would look like:
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);
}
});
With ES5 you can also do this via:
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'))With ES6 this would simply be:
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'))Please note that this is not a super performant approach and if you are going to search hundreds/thousands of documents this way it will not perform well. You would need to look into other ways (Binary search etc) to solve this.
Interesting they all work in the browser. What is the error for the lodash one? First suggestion.
– Akrion
Nov 14 '18 at 22:19
So you are telling me that if you add thesearchfunction from any of the top 2 code samples and in your code just change fromvar result = _.find(movies, {'title': name});tovar result = search(movies, name);it is not working and it gives an exception?
– Akrion
Nov 14 '18 at 22:25
I updated the 2nd paragraph. Hope this helps
– Akrion
Nov 14 '18 at 23:21
In your original post you had the collection named asmoviesso I left that.mvoiesshould be the name of the collection which you will be searching against.
– Akrion
Nov 14 '18 at 23:32
See comment. Search works just fine. Your logic is not correct.
– Akrion
Nov 14 '18 at 23:51
add a comment |
The issue with your code is that you are asking lodash to compare the exact contents of title and more importantly compare it while being case sensitive, since {title: name} triggers property match in lodash which simply compares the contents of title with the contents of the variable.
You can create a search function via lodash methods and no ES6 like this:
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>Using find, includes and toLower lodash methods.
Your code then would look like:
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);
}
});
With ES5 you can also do this via:
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'))With ES6 this would simply be:
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'))Please note that this is not a super performant approach and if you are going to search hundreds/thousands of documents this way it will not perform well. You would need to look into other ways (Binary search etc) to solve this.
Interesting they all work in the browser. What is the error for the lodash one? First suggestion.
– Akrion
Nov 14 '18 at 22:19
So you are telling me that if you add thesearchfunction from any of the top 2 code samples and in your code just change fromvar result = _.find(movies, {'title': name});tovar result = search(movies, name);it is not working and it gives an exception?
– Akrion
Nov 14 '18 at 22:25
I updated the 2nd paragraph. Hope this helps
– Akrion
Nov 14 '18 at 23:21
In your original post you had the collection named asmoviesso I left that.mvoiesshould be the name of the collection which you will be searching against.
– Akrion
Nov 14 '18 at 23:32
See comment. Search works just fine. Your logic is not correct.
– Akrion
Nov 14 '18 at 23:51
add a comment |
The issue with your code is that you are asking lodash to compare the exact contents of title and more importantly compare it while being case sensitive, since {title: name} triggers property match in lodash which simply compares the contents of title with the contents of the variable.
You can create a search function via lodash methods and no ES6 like this:
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>Using find, includes and toLower lodash methods.
Your code then would look like:
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);
}
});
With ES5 you can also do this via:
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'))With ES6 this would simply be:
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'))Please note that this is not a super performant approach and if you are going to search hundreds/thousands of documents this way it will not perform well. You would need to look into other ways (Binary search etc) to solve this.
The issue with your code is that you are asking lodash to compare the exact contents of title and more importantly compare it while being case sensitive, since {title: name} triggers property match in lodash which simply compares the contents of title with the contents of the variable.
You can create a search function via lodash methods and no ES6 like this:
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>Using find, includes and toLower lodash methods.
Your code then would look like:
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);
}
});
With ES5 you can also do this via:
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'))With ES6 this would simply be:
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'))Please note that this is not a super performant approach and if you are going to search hundreds/thousands of documents this way it will not perform well. You would need to look into other ways (Binary search etc) to solve this.
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>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>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'))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'))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'))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'))edited Nov 14 '18 at 23:36
answered Nov 14 '18 at 21:18
AkrionAkrion
9,40211224
9,40211224
Interesting they all work in the browser. What is the error for the lodash one? First suggestion.
– Akrion
Nov 14 '18 at 22:19
So you are telling me that if you add thesearchfunction from any of the top 2 code samples and in your code just change fromvar result = _.find(movies, {'title': name});tovar result = search(movies, name);it is not working and it gives an exception?
– Akrion
Nov 14 '18 at 22:25
I updated the 2nd paragraph. Hope this helps
– Akrion
Nov 14 '18 at 23:21
In your original post you had the collection named asmoviesso I left that.mvoiesshould be the name of the collection which you will be searching against.
– Akrion
Nov 14 '18 at 23:32
See comment. Search works just fine. Your logic is not correct.
– Akrion
Nov 14 '18 at 23:51
add a comment |
Interesting they all work in the browser. What is the error for the lodash one? First suggestion.
– Akrion
Nov 14 '18 at 22:19
So you are telling me that if you add thesearchfunction from any of the top 2 code samples and in your code just change fromvar result = _.find(movies, {'title': name});tovar result = search(movies, name);it is not working and it gives an exception?
– Akrion
Nov 14 '18 at 22:25
I updated the 2nd paragraph. Hope this helps
– Akrion
Nov 14 '18 at 23:21
In your original post you had the collection named asmoviesso I left that.mvoiesshould be the name of the collection which you will be searching against.
– Akrion
Nov 14 '18 at 23:32
See comment. Search works just fine. Your logic is not correct.
– Akrion
Nov 14 '18 at 23:51
Interesting they all work in the browser. What is the error for the lodash one? First suggestion.
– Akrion
Nov 14 '18 at 22:19
Interesting they all work in the browser. What is the error for the lodash one? First suggestion.
– Akrion
Nov 14 '18 at 22:19
So you are telling me that if you add the
search function from any of the top 2 code samples and in your code just change from var result = _.find(movies, {'title': name}); to var result = search(movies, name); it is not working and it gives an exception?– Akrion
Nov 14 '18 at 22:25
So you are telling me that if you add the
search function from any of the top 2 code samples and in your code just change from var result = _.find(movies, {'title': name}); to var result = search(movies, name); it is not working and it gives an exception?– Akrion
Nov 14 '18 at 22:25
I updated the 2nd paragraph. Hope this helps
– Akrion
Nov 14 '18 at 23:21
I updated the 2nd paragraph. Hope this helps
– Akrion
Nov 14 '18 at 23:21
In your original post you had the collection named as
movies so I left that. mvoies should be the name of the collection which you will be searching against.– Akrion
Nov 14 '18 at 23:32
In your original post you had the collection named as
movies so I left that. mvoies should be the name of the collection which you will be searching against.– Akrion
Nov 14 '18 at 23:32
See comment. Search works just fine. Your logic is not correct.
– Akrion
Nov 14 '18 at 23:51
See comment. Search works just fine. Your logic is not correct.
– Akrion
Nov 14 '18 at 23:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308477%2fhow-to-case-insentive-contain-search-with-lodash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@Akrion please see this related topic with different subject thanks so much : stackoverflow.com/questions/53312728/…
– Hamid
Nov 15 '18 at 5:13