在javascript对象

时间:2017-05-16 04:30:34

标签: javascript

我使用以下代码在javascript对象中查找特定的值id。我正在获取重复的对象。

这是我正在使用的javascript代码:

$('.seat').on('click', function(e) {
    e.preventDefault();
    e.stopPropagation();

   //SeatId is 1
    var categoryArray = jsonData;
    var id = $(this).attr('id');
    var ticketData, seatLocation;
    for (var i = 0; i < categoryArray.length; i++) {
            var category = categoryArray[i];
            for (var j = 0; j < category.assignments.length; j++) {
                if (category.assignments[j].seatId == id) {
                    ticketData = category;
                    seatLocation = category.assignments[j];
                }
            }
        }

    console.log(ticketData);
    console.log(seatLocation);

});

这是解析后目标的样子:

enter image description here

这就是数据的打印方式: enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

$('.seat').on('click', function (e) {
    e.preventDefault();
    e.stopPropagation();
                        var ticketData = [];
                    var seatLocation = [];
    //SeatId is 1
    var categoryArray = jsonData;
    var id = $(this).attr('id');
    $.each(categoryArray, function (i, cat) {
        $.each(cat.assignments, function (j, ass) {
            if (ass.seatId == id) {
                    ticketData = cat;
                    seatLocation = ass;
                }
            });
    });

    console.log(ticketData);
    console.log(seatLocation);

});