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













0















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'

);










share|improve this question



















  • 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 the forEach call.

    – T.J. Crowder
    Mar 7 at 13:04











  • No, there's a big difference between two ifs in a row and if/else if.

    – T.J. Crowder
    Mar 7 at 13:05















0















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'

);










share|improve this question



















  • 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 the forEach call.

    – T.J. Crowder
    Mar 7 at 13:04











  • No, there's a big difference between two ifs in a row and if/else if.

    – T.J. Crowder
    Mar 7 at 13:05













0












0








0








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'

);










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 the forEach call.

    – T.J. Crowder
    Mar 7 at 13:04











  • No, there's a big difference between two ifs in a row and if/else if.

    – T.J. Crowder
    Mar 7 at 13:05












  • 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 the forEach call.

    – T.J. Crowder
    Mar 7 at 13:04











  • No, there's a big difference between two ifs in a row and if/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 ifs in a row and if/else if.

– T.J. Crowder
Mar 7 at 13:05





No, there's a big difference between two ifs in a row and if/else if.

– T.J. Crowder
Mar 7 at 13:05












3 Answers
3






active

oldest

votes


















5














I see a couple of possible problems:



  1. 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.


  2. You've referred to an else, but there is no else in your code. You have two ifs in a row, but the result of the first if doesn't have any effect at all on the second one. You may have wanted else if. Then, the second if isn't performed if the condition in the first if 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;








share|improve this answer
































    0














    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).






    share|improve this answer























    • 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


















    0














    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






    share|improve this answer
























      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
      );



      );













      draft saved

      draft discarded


















      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









      5














      I see a couple of possible problems:



      1. 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.


      2. You've referred to an else, but there is no else in your code. You have two ifs in a row, but the result of the first if doesn't have any effect at all on the second one. You may have wanted else if. Then, the second if isn't performed if the condition in the first if 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;








      share|improve this answer





























        5














        I see a couple of possible problems:



        1. 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.


        2. You've referred to an else, but there is no else in your code. You have two ifs in a row, but the result of the first if doesn't have any effect at all on the second one. You may have wanted else if. Then, the second if isn't performed if the condition in the first if 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;








        share|improve this answer



























          5












          5








          5







          I see a couple of possible problems:



          1. 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.


          2. You've referred to an else, but there is no else in your code. You have two ifs in a row, but the result of the first if doesn't have any effect at all on the second one. You may have wanted else if. Then, the second if isn't performed if the condition in the first if 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;








          share|improve this answer















          I see a couple of possible problems:



          1. 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.


          2. You've referred to an else, but there is no else in your code. You have two ifs in a row, but the result of the first if doesn't have any effect at all on the second one. You may have wanted else if. Then, the second if isn't performed if the condition in the first if 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;









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 13:38

























          answered Mar 7 at 13:06









          T.J. CrowderT.J. Crowder

          696k12312401335




          696k12312401335























              0














              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).






              share|improve this answer























              • 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















              0














              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).






              share|improve this answer























              • 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













              0












              0








              0







              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).






              share|improve this answer













              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).







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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 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

















              • 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
















              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











              0














              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






              share|improve this answer





























                0














                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






                share|improve this answer



























                  0












                  0








                  0







                  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






                  share|improve this answer















                  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







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 7 at 13:33

























                  answered Mar 7 at 13:19









                  HMRHMR

                  14k1138102




                  14k1138102



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                      Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                      Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved