How can I push data into an array?
I have pushed some data into an array:
if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}
It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.
javascript jquery if-statement push
add a comment |
I have pushed some data into an array:
if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}
It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.
javascript jquery if-statement push
are you talking about errors array or form ?
– Dhiren
Nov 26 '18 at 12:00
about errors array
– Jarla
Nov 26 '18 at 12:01
looking at this tiny piece of code that you have shown doesn't need any optimization.
– Dhiren
Nov 26 '18 at 12:04
@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized
– Colin Cline
Nov 26 '18 at 12:06
@ColinCline correct.
– Dhiren
Nov 26 '18 at 12:07
add a comment |
I have pushed some data into an array:
if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}
It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.
javascript jquery if-statement push
I have pushed some data into an array:
if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}
It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.
javascript jquery if-statement push
javascript jquery if-statement push
edited Nov 26 '18 at 12:16
Andrei Suvorkov
4,1674929
4,1674929
asked Nov 26 '18 at 11:58
JarlaJarla
2,35711639
2,35711639
are you talking about errors array or form ?
– Dhiren
Nov 26 '18 at 12:00
about errors array
– Jarla
Nov 26 '18 at 12:01
looking at this tiny piece of code that you have shown doesn't need any optimization.
– Dhiren
Nov 26 '18 at 12:04
@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized
– Colin Cline
Nov 26 '18 at 12:06
@ColinCline correct.
– Dhiren
Nov 26 '18 at 12:07
add a comment |
are you talking about errors array or form ?
– Dhiren
Nov 26 '18 at 12:00
about errors array
– Jarla
Nov 26 '18 at 12:01
looking at this tiny piece of code that you have shown doesn't need any optimization.
– Dhiren
Nov 26 '18 at 12:04
@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized
– Colin Cline
Nov 26 '18 at 12:06
@ColinCline correct.
– Dhiren
Nov 26 '18 at 12:07
are you talking about errors array or form ?
– Dhiren
Nov 26 '18 at 12:00
are you talking about errors array or form ?
– Dhiren
Nov 26 '18 at 12:00
about errors array
– Jarla
Nov 26 '18 at 12:01
about errors array
– Jarla
Nov 26 '18 at 12:01
looking at this tiny piece of code that you have shown doesn't need any optimization.
– Dhiren
Nov 26 '18 at 12:04
looking at this tiny piece of code that you have shown doesn't need any optimization.
– Dhiren
Nov 26 '18 at 12:04
@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized
– Colin Cline
Nov 26 '18 at 12:06
@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized
– Colin Cline
Nov 26 '18 at 12:06
@ColinCline correct.
– Dhiren
Nov 26 '18 at 12:07
@ColinCline correct.
– Dhiren
Nov 26 '18 at 12:07
add a comment |
3 Answers
3
active
oldest
votes
If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
|| (name === 'form[plainPassword]' && target === 'create')){
errors.push(name);
}
Since:
name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.
In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all
add a comment |
I would do it in two methods
function CheckErrors(target,name){
switch(target){
case 'create':
SaveError(name,true);
break;
default:
SaveError(name);
break;
}
}
function SaveError(name,checkPassword){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
errors.push(name);
}
}
I am afraid you missed something
– Colin Cline
Nov 26 '18 at 12:07
just trying to figure out what i missed :)
– JustLearning
Nov 26 '18 at 12:09
OP dont want to push 'form[plainPassword]' in else statement
– Colin Cline
Nov 26 '18 at 12:11
1
indeed i see it. If i had to re-write it would still look ugly
– JustLearning
Nov 26 '18 at 12:13
add a comment |
You could use arrays to simplify your if statement:
if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
errors.push(name);
}
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%2f53480652%2fhow-can-i-push-data-into-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
|| (name === 'form[plainPassword]' && target === 'create')){
errors.push(name);
}
Since:
name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.
In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all
add a comment |
If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
|| (name === 'form[plainPassword]' && target === 'create')){
errors.push(name);
}
Since:
name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.
In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all
add a comment |
If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
|| (name === 'form[plainPassword]' && target === 'create')){
errors.push(name);
}
Since:
name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.
In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all
If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
|| (name === 'form[plainPassword]' && target === 'create')){
errors.push(name);
}
Since:
name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.
In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all
edited Nov 26 '18 at 12:09
answered Nov 26 '18 at 12:03
Andrei SuvorkovAndrei Suvorkov
4,1674929
4,1674929
add a comment |
add a comment |
I would do it in two methods
function CheckErrors(target,name){
switch(target){
case 'create':
SaveError(name,true);
break;
default:
SaveError(name);
break;
}
}
function SaveError(name,checkPassword){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
errors.push(name);
}
}
I am afraid you missed something
– Colin Cline
Nov 26 '18 at 12:07
just trying to figure out what i missed :)
– JustLearning
Nov 26 '18 at 12:09
OP dont want to push 'form[plainPassword]' in else statement
– Colin Cline
Nov 26 '18 at 12:11
1
indeed i see it. If i had to re-write it would still look ugly
– JustLearning
Nov 26 '18 at 12:13
add a comment |
I would do it in two methods
function CheckErrors(target,name){
switch(target){
case 'create':
SaveError(name,true);
break;
default:
SaveError(name);
break;
}
}
function SaveError(name,checkPassword){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
errors.push(name);
}
}
I am afraid you missed something
– Colin Cline
Nov 26 '18 at 12:07
just trying to figure out what i missed :)
– JustLearning
Nov 26 '18 at 12:09
OP dont want to push 'form[plainPassword]' in else statement
– Colin Cline
Nov 26 '18 at 12:11
1
indeed i see it. If i had to re-write it would still look ugly
– JustLearning
Nov 26 '18 at 12:13
add a comment |
I would do it in two methods
function CheckErrors(target,name){
switch(target){
case 'create':
SaveError(name,true);
break;
default:
SaveError(name);
break;
}
}
function SaveError(name,checkPassword){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
errors.push(name);
}
}
I would do it in two methods
function CheckErrors(target,name){
switch(target){
case 'create':
SaveError(name,true);
break;
default:
SaveError(name);
break;
}
}
function SaveError(name,checkPassword){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
errors.push(name);
}
}
edited Nov 26 '18 at 12:14
answered Nov 26 '18 at 12:07
JustLearningJustLearning
1,05821636
1,05821636
I am afraid you missed something
– Colin Cline
Nov 26 '18 at 12:07
just trying to figure out what i missed :)
– JustLearning
Nov 26 '18 at 12:09
OP dont want to push 'form[plainPassword]' in else statement
– Colin Cline
Nov 26 '18 at 12:11
1
indeed i see it. If i had to re-write it would still look ugly
– JustLearning
Nov 26 '18 at 12:13
add a comment |
I am afraid you missed something
– Colin Cline
Nov 26 '18 at 12:07
just trying to figure out what i missed :)
– JustLearning
Nov 26 '18 at 12:09
OP dont want to push 'form[plainPassword]' in else statement
– Colin Cline
Nov 26 '18 at 12:11
1
indeed i see it. If i had to re-write it would still look ugly
– JustLearning
Nov 26 '18 at 12:13
I am afraid you missed something
– Colin Cline
Nov 26 '18 at 12:07
I am afraid you missed something
– Colin Cline
Nov 26 '18 at 12:07
just trying to figure out what i missed :)
– JustLearning
Nov 26 '18 at 12:09
just trying to figure out what i missed :)
– JustLearning
Nov 26 '18 at 12:09
OP dont want to push 'form[plainPassword]' in else statement
– Colin Cline
Nov 26 '18 at 12:11
OP dont want to push 'form[plainPassword]' in else statement
– Colin Cline
Nov 26 '18 at 12:11
1
1
indeed i see it. If i had to re-write it would still look ugly
– JustLearning
Nov 26 '18 at 12:13
indeed i see it. If i had to re-write it would still look ugly
– JustLearning
Nov 26 '18 at 12:13
add a comment |
You could use arrays to simplify your if statement:
if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
errors.push(name);
}
add a comment |
You could use arrays to simplify your if statement:
if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
errors.push(name);
}
add a comment |
You could use arrays to simplify your if statement:
if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
errors.push(name);
}
You could use arrays to simplify your if statement:
if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
errors.push(name);
}
answered Nov 26 '18 at 12:18
XorifelseXorifelse
6,48311934
6,48311934
add a comment |
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%2f53480652%2fhow-can-i-push-data-into-an-array%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
are you talking about errors array or form ?
– Dhiren
Nov 26 '18 at 12:00
about errors array
– Jarla
Nov 26 '18 at 12:01
looking at this tiny piece of code that you have shown doesn't need any optimization.
– Dhiren
Nov 26 '18 at 12:04
@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized
– Colin Cline
Nov 26 '18 at 12:06
@ColinCline correct.
– Dhiren
Nov 26 '18 at 12:07