Categorise string based on keyword
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a string that can be anything.
I have a bunch of different functions which will be called dependent on the keywords in the string. Only the first matched keyword will be applicable, so no need for a use case of multiple matches.
Let's say I have this logic
- if string includes 'curtains', call someCurtainFunction()
- if string includes 'cheese', call someCheeseFunction()
Right now I am doing this:
if(someString.includes('curtains')){
someCurtainFunction(params);
} else if(someString.includes('cheese')){
someCheeseFunction(params);
}
And that's fine - it works. But the list is quite long. It isn't the most readable.
So I thought about something like this, but it seems a bit bad practise to use eval()
var keywords = {
'curtains': 'someCurtainFunction(params)',
'cheese': 'someCheeseFunction(params)'
}
for(var keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
eval(keywords[keyword]);
break;
}
}
How best would I tackle this issue without eval?
javascript
add a comment |
I have a string that can be anything.
I have a bunch of different functions which will be called dependent on the keywords in the string. Only the first matched keyword will be applicable, so no need for a use case of multiple matches.
Let's say I have this logic
- if string includes 'curtains', call someCurtainFunction()
- if string includes 'cheese', call someCheeseFunction()
Right now I am doing this:
if(someString.includes('curtains')){
someCurtainFunction(params);
} else if(someString.includes('cheese')){
someCheeseFunction(params);
}
And that's fine - it works. But the list is quite long. It isn't the most readable.
So I thought about something like this, but it seems a bit bad practise to use eval()
var keywords = {
'curtains': 'someCurtainFunction(params)',
'cheese': 'someCheeseFunction(params)'
}
for(var keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
eval(keywords[keyword]);
break;
}
}
How best would I tackle this issue without eval?
javascript
1
SO just reference the function
– epascarello
Nov 16 '18 at 19:18
@epascarello they need to be able to pass in parameters
– Joe Iddon
Nov 16 '18 at 19:18
So than add an object with additional parameters
– epascarello
Nov 16 '18 at 19:19
add a comment |
I have a string that can be anything.
I have a bunch of different functions which will be called dependent on the keywords in the string. Only the first matched keyword will be applicable, so no need for a use case of multiple matches.
Let's say I have this logic
- if string includes 'curtains', call someCurtainFunction()
- if string includes 'cheese', call someCheeseFunction()
Right now I am doing this:
if(someString.includes('curtains')){
someCurtainFunction(params);
} else if(someString.includes('cheese')){
someCheeseFunction(params);
}
And that's fine - it works. But the list is quite long. It isn't the most readable.
So I thought about something like this, but it seems a bit bad practise to use eval()
var keywords = {
'curtains': 'someCurtainFunction(params)',
'cheese': 'someCheeseFunction(params)'
}
for(var keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
eval(keywords[keyword]);
break;
}
}
How best would I tackle this issue without eval?
javascript
I have a string that can be anything.
I have a bunch of different functions which will be called dependent on the keywords in the string. Only the first matched keyword will be applicable, so no need for a use case of multiple matches.
Let's say I have this logic
- if string includes 'curtains', call someCurtainFunction()
- if string includes 'cheese', call someCheeseFunction()
Right now I am doing this:
if(someString.includes('curtains')){
someCurtainFunction(params);
} else if(someString.includes('cheese')){
someCheeseFunction(params);
}
And that's fine - it works. But the list is quite long. It isn't the most readable.
So I thought about something like this, but it seems a bit bad practise to use eval()
var keywords = {
'curtains': 'someCurtainFunction(params)',
'cheese': 'someCheeseFunction(params)'
}
for(var keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
eval(keywords[keyword]);
break;
}
}
How best would I tackle this issue without eval?
javascript
javascript
asked Nov 16 '18 at 19:16
Bob HoskinsBob Hoskins
153
153
1
SO just reference the function
– epascarello
Nov 16 '18 at 19:18
@epascarello they need to be able to pass in parameters
– Joe Iddon
Nov 16 '18 at 19:18
So than add an object with additional parameters
– epascarello
Nov 16 '18 at 19:19
add a comment |
1
SO just reference the function
– epascarello
Nov 16 '18 at 19:18
@epascarello they need to be able to pass in parameters
– Joe Iddon
Nov 16 '18 at 19:18
So than add an object with additional parameters
– epascarello
Nov 16 '18 at 19:19
1
1
SO just reference the function
– epascarello
Nov 16 '18 at 19:18
SO just reference the function
– epascarello
Nov 16 '18 at 19:18
@epascarello they need to be able to pass in parameters
– Joe Iddon
Nov 16 '18 at 19:18
@epascarello they need to be able to pass in parameters
– Joe Iddon
Nov 16 '18 at 19:18
So than add an object with additional parameters
– epascarello
Nov 16 '18 at 19:19
So than add an object with additional parameters
– epascarello
Nov 16 '18 at 19:19
add a comment |
1 Answer
1
active
oldest
votes
I would suggest arrow functions if params change:
var keywords = {
'curtains': ()=>someCurtainFunction(params),
'cheese': ()=>someCheeseFunction(params)
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword]();
break;
}
}
Otherwise, if all the functions are called with the same params, just store a reference to the functions:
var keywords = {
'curtains': someCurtainFunction,
'cheese': someCheeseFunction
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword](params);
break;
}
}
this is a good answer
– sgtkuncoro
Nov 16 '18 at 19:24
Yes params will change. Thanks that's a novel approach.
– Bob Hoskins
Nov 16 '18 at 19:37
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%2f53344084%2fcategorise-string-based-on-keyword%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
I would suggest arrow functions if params change:
var keywords = {
'curtains': ()=>someCurtainFunction(params),
'cheese': ()=>someCheeseFunction(params)
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword]();
break;
}
}
Otherwise, if all the functions are called with the same params, just store a reference to the functions:
var keywords = {
'curtains': someCurtainFunction,
'cheese': someCheeseFunction
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword](params);
break;
}
}
this is a good answer
– sgtkuncoro
Nov 16 '18 at 19:24
Yes params will change. Thanks that's a novel approach.
– Bob Hoskins
Nov 16 '18 at 19:37
add a comment |
I would suggest arrow functions if params change:
var keywords = {
'curtains': ()=>someCurtainFunction(params),
'cheese': ()=>someCheeseFunction(params)
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword]();
break;
}
}
Otherwise, if all the functions are called with the same params, just store a reference to the functions:
var keywords = {
'curtains': someCurtainFunction,
'cheese': someCheeseFunction
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword](params);
break;
}
}
this is a good answer
– sgtkuncoro
Nov 16 '18 at 19:24
Yes params will change. Thanks that's a novel approach.
– Bob Hoskins
Nov 16 '18 at 19:37
add a comment |
I would suggest arrow functions if params change:
var keywords = {
'curtains': ()=>someCurtainFunction(params),
'cheese': ()=>someCheeseFunction(params)
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword]();
break;
}
}
Otherwise, if all the functions are called with the same params, just store a reference to the functions:
var keywords = {
'curtains': someCurtainFunction,
'cheese': someCheeseFunction
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword](params);
break;
}
}
I would suggest arrow functions if params change:
var keywords = {
'curtains': ()=>someCurtainFunction(params),
'cheese': ()=>someCheeseFunction(params)
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword]();
break;
}
}
Otherwise, if all the functions are called with the same params, just store a reference to the functions:
var keywords = {
'curtains': someCurtainFunction,
'cheese': someCheeseFunction
}
for (let keyword of Object.keys(keywords)){
if(someString.includes(keyword)){
keywords[keyword](params);
break;
}
}
answered Nov 16 '18 at 19:20
Joe IddonJoe Iddon
15.6k31741
15.6k31741
this is a good answer
– sgtkuncoro
Nov 16 '18 at 19:24
Yes params will change. Thanks that's a novel approach.
– Bob Hoskins
Nov 16 '18 at 19:37
add a comment |
this is a good answer
– sgtkuncoro
Nov 16 '18 at 19:24
Yes params will change. Thanks that's a novel approach.
– Bob Hoskins
Nov 16 '18 at 19:37
this is a good answer
– sgtkuncoro
Nov 16 '18 at 19:24
this is a good answer
– sgtkuncoro
Nov 16 '18 at 19:24
Yes params will change. Thanks that's a novel approach.
– Bob Hoskins
Nov 16 '18 at 19:37
Yes params will change. Thanks that's a novel approach.
– Bob Hoskins
Nov 16 '18 at 19:37
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%2f53344084%2fcategorise-string-based-on-keyword%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
1
SO just reference the function
– epascarello
Nov 16 '18 at 19:18
@epascarello they need to be able to pass in parameters
– Joe Iddon
Nov 16 '18 at 19:18
So than add an object with additional parameters
– epascarello
Nov 16 '18 at 19:19