Copy link to clipboard
Copied
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?
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
hi sinious,
Thanks - that makes sense.
Copy link to clipboard
Copied
Cool - I see that I didn't have the correct syntax like you had
--- if (value.name == 'Bob') { output += ' nice name'; }
Copy link to clipboard
Copied
You're welcome!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more