if /else condition inside foreachJavaScript closure inside loops – simple practical exampleLogical operator in a handlebars.js #if conditionalHow to access the correct `this` inside a callback?Loop inside React JSXLogin still directs even if incorrect credential with AngularJS and PHPUsing async/await with a forEach loopHow to use *ngIf else?Angular MEAN stack: can't acces files added during runtime (with multer) without serving againAngular Material Table won't populate with external data on initial loadAngular: Image in assets not loading immediately
Why escape if the_content isnt?
Would a high gravity rocky planet be guaranteed to have an atmosphere?
Is exact Kanji stroke length important?
What can we do to stop prior company from asking us questions?
Is a stroke of luck acceptable after a series of unfavorable events?
What Brexit proposals are on the table in the indicative votes on the 27th of March 2019?
Do the temporary hit points from Reckless Abandon stack if I make multiple attacks on my turn?
What is the intuitive meaning of having a linear relationship between the logs of two variables?
How can a function with a hole (removable discontinuity) equal a function with no hole?
Why are there no referendums in the US?
Was Spock the First Vulcan in Starfleet?
How to draw lines on a tikz-cd diagram
Is expanding the research of a group into machine learning as a PhD student risky?
What does "I’d sit this one out, Cap," imply or mean in the context?
How to safely derail a train during transit?
CREATE opcode: what does it really do?
Integer addition + constant, is it a group?
How to Reset Passwords on Multiple Websites Easily?
Gears on left are inverse to gears on right?
Crossing the line between justified force and brutality
How do I extract a value from a time formatted value in excel?
Method to test if a number is a perfect power?
Is HostGator storing my password in plaintext?
How do I go from 300 unfinished/half written blog posts, to published posts?
if /else condition inside foreach
JavaScript closure inside loops – simple practical exampleLogical operator in a handlebars.js #if conditionalHow to access the correct `this` inside a callback?Loop inside React JSXLogin still directs even if incorrect credential with AngularJS and PHPUsing async/await with a forEach loopHow to use *ngIf else?Angular MEAN stack: can't acces files added during runtime (with multer) without serving againAngular Material Table won't populate with external data on initial loadAngular: Image in assets not loading immediately
I am developing an angular project.
I have a table file I want to compare each element with a data value if it is correct I will do statement else I will do another comportement but my problem is that even the data is correct it always fetches all the table and should access in the else for a very short time.
How to avoid that, please.
Here is my code:
if (this.data)
this.imgNotFoundText = '';
this.data.package.files.forEach(element =>
i++;
this.picture = '';
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
javascript angular typescript
|
show 3 more comments
I am developing an angular project.
I have a table file I want to compare each element with a data value if it is correct I will do statement else I will do another comportement but my problem is that even the data is correct it always fetches all the table and should access in the else for a very short time.
How to avoid that, please.
Here is my code:
if (this.data)
this.imgNotFoundText = '';
this.data.package.files.forEach(element =>
i++;
this.picture = '';
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
javascript angular typescript
3
"it always fetch all the table and should access in the else for a very short time" There is noelse
in your code...?
– T.J. Crowder
Mar 7 at 13:02
1
When asking for help, it's best to indent your code in a consistent, fairly standard way, as it helps people read and understand your code. (It's useful when you're not asking for help, too.)
– T.J. Crowder
Mar 7 at 13:03
the else is the second if
– Mat
Mar 7 at 13:04
1
Separately: It seems suspect to always dothis.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.
– T.J. Crowder
Mar 7 at 13:04
No, there's a big difference between twoif
s in a row andif
/else if
.
– T.J. Crowder
Mar 7 at 13:05
|
show 3 more comments
I am developing an angular project.
I have a table file I want to compare each element with a data value if it is correct I will do statement else I will do another comportement but my problem is that even the data is correct it always fetches all the table and should access in the else for a very short time.
How to avoid that, please.
Here is my code:
if (this.data)
this.imgNotFoundText = '';
this.data.package.files.forEach(element =>
i++;
this.picture = '';
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
javascript angular typescript
I am developing an angular project.
I have a table file I want to compare each element with a data value if it is correct I will do statement else I will do another comportement but my problem is that even the data is correct it always fetches all the table and should access in the else for a very short time.
How to avoid that, please.
Here is my code:
if (this.data)
this.imgNotFoundText = '';
this.data.package.files.forEach(element =>
i++;
this.picture = '';
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
javascript angular typescript
javascript angular typescript
edited Mar 7 at 13:42
Cristian Traìna
2,93011628
2,93011628
asked Mar 7 at 13:00
MatMat
73211
73211
3
"it always fetch all the table and should access in the else for a very short time" There is noelse
in your code...?
– T.J. Crowder
Mar 7 at 13:02
1
When asking for help, it's best to indent your code in a consistent, fairly standard way, as it helps people read and understand your code. (It's useful when you're not asking for help, too.)
– T.J. Crowder
Mar 7 at 13:03
the else is the second if
– Mat
Mar 7 at 13:04
1
Separately: It seems suspect to always dothis.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.
– T.J. Crowder
Mar 7 at 13:04
No, there's a big difference between twoif
s in a row andif
/else if
.
– T.J. Crowder
Mar 7 at 13:05
|
show 3 more comments
3
"it always fetch all the table and should access in the else for a very short time" There is noelse
in your code...?
– T.J. Crowder
Mar 7 at 13:02
1
When asking for help, it's best to indent your code in a consistent, fairly standard way, as it helps people read and understand your code. (It's useful when you're not asking for help, too.)
– T.J. Crowder
Mar 7 at 13:03
the else is the second if
– Mat
Mar 7 at 13:04
1
Separately: It seems suspect to always dothis.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.
– T.J. Crowder
Mar 7 at 13:04
No, there's a big difference between twoif
s in a row andif
/else if
.
– T.J. Crowder
Mar 7 at 13:05
3
3
"it always fetch all the table and should access in the else for a very short time" There is no
else
in your code...?– T.J. Crowder
Mar 7 at 13:02
"it always fetch all the table and should access in the else for a very short time" There is no
else
in your code...?– T.J. Crowder
Mar 7 at 13:02
1
1
When asking for help, it's best to indent your code in a consistent, fairly standard way, as it helps people read and understand your code. (It's useful when you're not asking for help, too.)
– T.J. Crowder
Mar 7 at 13:03
When asking for help, it's best to indent your code in a consistent, fairly standard way, as it helps people read and understand your code. (It's useful when you're not asking for help, too.)
– T.J. Crowder
Mar 7 at 13:03
the else is the second if
– Mat
Mar 7 at 13:04
the else is the second if
– Mat
Mar 7 at 13:04
1
1
Separately: It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before the forEach
call.– T.J. Crowder
Mar 7 at 13:04
Separately: It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before the forEach
call.– T.J. Crowder
Mar 7 at 13:04
No, there's a big difference between two
if
s in a row and if
/else if
.– T.J. Crowder
Mar 7 at 13:05
No, there's a big difference between two
if
s in a row and if
/else if
.– T.J. Crowder
Mar 7 at 13:05
|
show 3 more comments
3 Answers
3
active
oldest
votes
I see a couple of possible problems:
It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.You've referred to an
else
, but there is noelse
in your code. You have twoif
s in a row, but the result of the firstif
doesn't have any effect at all on the second one. You may have wantedelse if
. Then, the secondif
isn't performed if the condition in the firstif
was true.
So if both of those guesses are right:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach(element =>
i++;
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
Side note: You haven't shown how i
is initialized, but if it's used to track the index of the current entry of the forEach
, there's no need: forEach
receives that as a second argument:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach((element, index) =>
// -----------------------------^^^^^^^^^^^^^^^^
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (index == this.data.package.files.length && this.picture == '')
// ------------^^^^^
this.picture = './../assets/img/notFound.jpg'
);
You also might want to avoid that second if
altogether, by just specifying the "not found" default before the loop:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
files.forEach(element =>
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
);
In that I've assumed this.picture
should be ''
if there are no entries in files
, or the "not found" image if there's at least one entry. The loop will overwrite it if it finds a match.
Continuing from there, unless there can be multiple entries in files
with the same name
, you probably want to stop as of the first match. So:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
for (const name of files)
if (name == this.data.properties.Name)
this.picture = 'picOne.png'
break;
add a comment |
Not sure what your goal is but you're just iterating through the list and setting the picture value every time, this means you'll have as result the last element's picture value.
If your goal is to display a "not found" picture for elements who don't have files, you need to have an array of pictures that reflects the array of files (or add a property picture to each file).
my problem that even i have a file the notFound picture is displayed for a short time then the right picture displayed
– Mat
Mar 7 at 13:11
This suggests that you should use a temporary variable to storing the result, and assign it tothis.picture
only after the cycle is done. Also abreak
statement at the first successful result should be used as in previous answer.
– Massimiliano Sartoretto
Mar 7 at 13:14
add a comment |
Looks like you can use Array.prototype.some
if (this.data)
this.imgNotFoundText = '';
this.picture = this.data.package.files.some(
(element) => element.name === this.data.properties.Name
) ? 'picOne.png' : './../assets/img/notFound.jpg'
It'll return picOne.png
if any of the items in data.package.files has a name that is same as this.data.properties.Name or it'll return notFound.jpg
Although that could
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%2f55044413%2fif-else-condition-inside-foreach%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
I see a couple of possible problems:
It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.You've referred to an
else
, but there is noelse
in your code. You have twoif
s in a row, but the result of the firstif
doesn't have any effect at all on the second one. You may have wantedelse if
. Then, the secondif
isn't performed if the condition in the firstif
was true.
So if both of those guesses are right:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach(element =>
i++;
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
Side note: You haven't shown how i
is initialized, but if it's used to track the index of the current entry of the forEach
, there's no need: forEach
receives that as a second argument:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach((element, index) =>
// -----------------------------^^^^^^^^^^^^^^^^
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (index == this.data.package.files.length && this.picture == '')
// ------------^^^^^
this.picture = './../assets/img/notFound.jpg'
);
You also might want to avoid that second if
altogether, by just specifying the "not found" default before the loop:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
files.forEach(element =>
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
);
In that I've assumed this.picture
should be ''
if there are no entries in files
, or the "not found" image if there's at least one entry. The loop will overwrite it if it finds a match.
Continuing from there, unless there can be multiple entries in files
with the same name
, you probably want to stop as of the first match. So:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
for (const name of files)
if (name == this.data.properties.Name)
this.picture = 'picOne.png'
break;
add a comment |
I see a couple of possible problems:
It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.You've referred to an
else
, but there is noelse
in your code. You have twoif
s in a row, but the result of the firstif
doesn't have any effect at all on the second one. You may have wantedelse if
. Then, the secondif
isn't performed if the condition in the firstif
was true.
So if both of those guesses are right:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach(element =>
i++;
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
Side note: You haven't shown how i
is initialized, but if it's used to track the index of the current entry of the forEach
, there's no need: forEach
receives that as a second argument:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach((element, index) =>
// -----------------------------^^^^^^^^^^^^^^^^
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (index == this.data.package.files.length && this.picture == '')
// ------------^^^^^
this.picture = './../assets/img/notFound.jpg'
);
You also might want to avoid that second if
altogether, by just specifying the "not found" default before the loop:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
files.forEach(element =>
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
);
In that I've assumed this.picture
should be ''
if there are no entries in files
, or the "not found" image if there's at least one entry. The loop will overwrite it if it finds a match.
Continuing from there, unless there can be multiple entries in files
with the same name
, you probably want to stop as of the first match. So:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
for (const name of files)
if (name == this.data.properties.Name)
this.picture = 'picOne.png'
break;
add a comment |
I see a couple of possible problems:
It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.You've referred to an
else
, but there is noelse
in your code. You have twoif
s in a row, but the result of the firstif
doesn't have any effect at all on the second one. You may have wantedelse if
. Then, the secondif
isn't performed if the condition in the firstif
was true.
So if both of those guesses are right:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach(element =>
i++;
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
Side note: You haven't shown how i
is initialized, but if it's used to track the index of the current entry of the forEach
, there's no need: forEach
receives that as a second argument:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach((element, index) =>
// -----------------------------^^^^^^^^^^^^^^^^
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (index == this.data.package.files.length && this.picture == '')
// ------------^^^^^
this.picture = './../assets/img/notFound.jpg'
);
You also might want to avoid that second if
altogether, by just specifying the "not found" default before the loop:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
files.forEach(element =>
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
);
In that I've assumed this.picture
should be ''
if there are no entries in files
, or the "not found" image if there's at least one entry. The loop will overwrite it if it finds a match.
Continuing from there, unless there can be multiple entries in files
with the same name
, you probably want to stop as of the first match. So:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
for (const name of files)
if (name == this.data.properties.Name)
this.picture = 'picOne.png'
break;
I see a couple of possible problems:
It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.You've referred to an
else
, but there is noelse
in your code. You have twoif
s in a row, but the result of the firstif
doesn't have any effect at all on the second one. You may have wantedelse if
. Then, the secondif
isn't performed if the condition in the firstif
was true.
So if both of those guesses are right:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach(element =>
i++;
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (i == this.data.package.files.length && this.picture == '')
this.picture = './../assets/img/notFound.jpg'
);
Side note: You haven't shown how i
is initialized, but if it's used to track the index of the current entry of the forEach
, there's no need: forEach
receives that as a second argument:
if (this.data)
this.imgNotFoundText = '';
this.picture = '';
this.data.package.files.forEach((element, index) =>
// -----------------------------^^^^^^^^^^^^^^^^
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
else if (index == this.data.package.files.length && this.picture == '')
// ------------^^^^^
this.picture = './../assets/img/notFound.jpg'
);
You also might want to avoid that second if
altogether, by just specifying the "not found" default before the loop:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
files.forEach(element =>
if (element.name == this.data.properties.Name)
this.picture = 'picOne.png'
);
In that I've assumed this.picture
should be ''
if there are no entries in files
, or the "not found" image if there's at least one entry. The loop will overwrite it if it finds a match.
Continuing from there, unless there can be multiple entries in files
with the same name
, you probably want to stop as of the first match. So:
if (this.data)
this.imgNotFoundText = '';
const files = this.data.package;
this.picture = files.length ? './../assets/img/notFound.jpg' : '';
for (const name of files)
if (name == this.data.properties.Name)
this.picture = 'picOne.png'
break;
edited Mar 7 at 13:38
answered Mar 7 at 13:06
T.J. CrowderT.J. Crowder
696k12312401335
696k12312401335
add a comment |
add a comment |
Not sure what your goal is but you're just iterating through the list and setting the picture value every time, this means you'll have as result the last element's picture value.
If your goal is to display a "not found" picture for elements who don't have files, you need to have an array of pictures that reflects the array of files (or add a property picture to each file).
my problem that even i have a file the notFound picture is displayed for a short time then the right picture displayed
– Mat
Mar 7 at 13:11
This suggests that you should use a temporary variable to storing the result, and assign it tothis.picture
only after the cycle is done. Also abreak
statement at the first successful result should be used as in previous answer.
– Massimiliano Sartoretto
Mar 7 at 13:14
add a comment |
Not sure what your goal is but you're just iterating through the list and setting the picture value every time, this means you'll have as result the last element's picture value.
If your goal is to display a "not found" picture for elements who don't have files, you need to have an array of pictures that reflects the array of files (or add a property picture to each file).
my problem that even i have a file the notFound picture is displayed for a short time then the right picture displayed
– Mat
Mar 7 at 13:11
This suggests that you should use a temporary variable to storing the result, and assign it tothis.picture
only after the cycle is done. Also abreak
statement at the first successful result should be used as in previous answer.
– Massimiliano Sartoretto
Mar 7 at 13:14
add a comment |
Not sure what your goal is but you're just iterating through the list and setting the picture value every time, this means you'll have as result the last element's picture value.
If your goal is to display a "not found" picture for elements who don't have files, you need to have an array of pictures that reflects the array of files (or add a property picture to each file).
Not sure what your goal is but you're just iterating through the list and setting the picture value every time, this means you'll have as result the last element's picture value.
If your goal is to display a "not found" picture for elements who don't have files, you need to have an array of pictures that reflects the array of files (or add a property picture to each file).
answered Mar 7 at 13:07
Massimiliano SartorettoMassimiliano Sartoretto
506210
506210
my problem that even i have a file the notFound picture is displayed for a short time then the right picture displayed
– Mat
Mar 7 at 13:11
This suggests that you should use a temporary variable to storing the result, and assign it tothis.picture
only after the cycle is done. Also abreak
statement at the first successful result should be used as in previous answer.
– Massimiliano Sartoretto
Mar 7 at 13:14
add a comment |
my problem that even i have a file the notFound picture is displayed for a short time then the right picture displayed
– Mat
Mar 7 at 13:11
This suggests that you should use a temporary variable to storing the result, and assign it tothis.picture
only after the cycle is done. Also abreak
statement at the first successful result should be used as in previous answer.
– Massimiliano Sartoretto
Mar 7 at 13:14
my problem that even i have a file the notFound picture is displayed for a short time then the right picture displayed
– Mat
Mar 7 at 13:11
my problem that even i have a file the notFound picture is displayed for a short time then the right picture displayed
– Mat
Mar 7 at 13:11
This suggests that you should use a temporary variable to storing the result, and assign it to
this.picture
only after the cycle is done. Also a break
statement at the first successful result should be used as in previous answer.– Massimiliano Sartoretto
Mar 7 at 13:14
This suggests that you should use a temporary variable to storing the result, and assign it to
this.picture
only after the cycle is done. Also a break
statement at the first successful result should be used as in previous answer.– Massimiliano Sartoretto
Mar 7 at 13:14
add a comment |
Looks like you can use Array.prototype.some
if (this.data)
this.imgNotFoundText = '';
this.picture = this.data.package.files.some(
(element) => element.name === this.data.properties.Name
) ? 'picOne.png' : './../assets/img/notFound.jpg'
It'll return picOne.png
if any of the items in data.package.files has a name that is same as this.data.properties.Name or it'll return notFound.jpg
Although that could
add a comment |
Looks like you can use Array.prototype.some
if (this.data)
this.imgNotFoundText = '';
this.picture = this.data.package.files.some(
(element) => element.name === this.data.properties.Name
) ? 'picOne.png' : './../assets/img/notFound.jpg'
It'll return picOne.png
if any of the items in data.package.files has a name that is same as this.data.properties.Name or it'll return notFound.jpg
Although that could
add a comment |
Looks like you can use Array.prototype.some
if (this.data)
this.imgNotFoundText = '';
this.picture = this.data.package.files.some(
(element) => element.name === this.data.properties.Name
) ? 'picOne.png' : './../assets/img/notFound.jpg'
It'll return picOne.png
if any of the items in data.package.files has a name that is same as this.data.properties.Name or it'll return notFound.jpg
Although that could
Looks like you can use Array.prototype.some
if (this.data)
this.imgNotFoundText = '';
this.picture = this.data.package.files.some(
(element) => element.name === this.data.properties.Name
) ? 'picOne.png' : './../assets/img/notFound.jpg'
It'll return picOne.png
if any of the items in data.package.files has a name that is same as this.data.properties.Name or it'll return notFound.jpg
Although that could
edited Mar 7 at 13:33
answered Mar 7 at 13:19
HMRHMR
14k1138102
14k1138102
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%2f55044413%2fif-else-condition-inside-foreach%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
3
"it always fetch all the table and should access in the else for a very short time" There is no
else
in your code...?– T.J. Crowder
Mar 7 at 13:02
1
When asking for help, it's best to indent your code in a consistent, fairly standard way, as it helps people read and understand your code. (It's useful when you're not asking for help, too.)
– T.J. Crowder
Mar 7 at 13:03
the else is the second if
– Mat
Mar 7 at 13:04
1
Separately: It seems suspect to always do
this.picture = '';
unconditionally in the loop. If you're going to do that, you may as well only look at the last entry in the array. You probably want to move that to before theforEach
call.– T.J. Crowder
Mar 7 at 13:04
No, there's a big difference between two
if
s in a row andif
/else if
.– T.J. Crowder
Mar 7 at 13:05