JSON jQuery if заявление

0

Я хочу иметь возможность добавить значение 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>');
        });
    });
});
  • 3
    Где, if statement ?
  • 0
    это не там, это то, что я ищу :)
Показать ещё 3 комментария
Теги:
if-statement

1 ответ

1
Лучший ответ

Как насчет:

$.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>');
    }
});
  • 0
    Спасибо, это прекрасно работает !!!!!!!!

Ещё вопросы

Сообщество Overcoder
Наверх
Меню