Я хочу иметь возможность добавить значение title_coupon
из объекта deal.options
но только если он существует. Если options
нет, я хочу получить title.coupon
from data.deal
. Приведенный ниже код работает, но он, очевидно, возвращает title.coupon
из обоих deal.options
и data.deal
. Может ли кто-нибудь помочь мне построить оператор if?
Это мой текущий код:
$.each(data.deal, function (x, deal) {
$('#container').after('<tr><td>' + this.title_coupon + '</td><td>' + this.id + '</td></tr>');
$.each(deal.options, function (x, options) {
$.each(options, function (x, deal) {
$('#container').append('<tr><td>' + this.title_coupon + '</td><td>' + this.id + '</td></tr>');
});
});
});
Как насчет:
$.each(data.deal, function (x, deal) {
var found = false;
$.each(deal.options, function (x, options) {
$.each(options, function (x, deal) {
$('#container').append('<tr><td>' + this.title_coupon + '</td><td>' + this.id + '</td></tr>');
found = true;
});
});
if (!found) {
$('#container').after('<tr><td>' + this.title_coupon + '</td><td>' + this.id + '</td></tr>');
}
});
if statement
?