Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

use if/then inside .each loop

Contributor ,
Oct 06, 2015 Oct 06, 2015

hi all,

still a javascript jQuery newbie...

I want to add text inside the .each loop using an if/then statement - not sure how to do it...

basic.

$.each(data, function(key, value) {

            console. log ('item', key, value);

    output += "<li>" +

  value.id + " " + value.name + " " + value.date

  + "</li>";

  });

I tried this IF... : but doesn't work...

$.each(data, function(key, value) {

              console. log ('item', key, value);

      output += "<li>" +

  value.id + " " + value.name

  if (value.name == "Bob") {

  + " nice name "

  }

  " " + value.date

  + "</li>";

  });

Q: Any idea how to get it to work?

748
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 07, 2015 Oct 07, 2015

You're already performing the concatenation when you do this: output +=

You know that it will keep what already exists in output and add to it (assuming this is assigned outside the loop). So you can just simply add more of the same thing.

Inside the loop:

output += '<li>' + value.id + ' ' + value.name;

if (value.name == 'Bob') { output += ' nice name'; }

output += ' ' + value.date + '</li>';

Concatenate as much as you like. Unless you're dealing with crazy amounts of data, for a new person the p

...
Translate
LEGEND ,
Oct 07, 2015 Oct 07, 2015

You're already performing the concatenation when you do this: output +=

You know that it will keep what already exists in output and add to it (assuming this is assigned outside the loop). So you can just simply add more of the same thing.

Inside the loop:

output += '<li>' + value.id + ' ' + value.name;

if (value.name == 'Bob') { output += ' nice name'; }

output += ' ' + value.date + '</li>';

Concatenate as much as you like. Unless you're dealing with crazy amounts of data, for a new person the performance impact of several concatenations is negligible.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 10, 2015 Oct 10, 2015

hi sinious,

Thanks - that makes sense.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 10, 2015 Oct 10, 2015

Cool - I see that I didn't have the correct syntax like you had

--- if (value.name == 'Bob') { output += ' nice name'; }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 12, 2015 Oct 12, 2015
LATEST

You're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines