无法迭代到javascript对象数组中

时间:2016-10-26 14:37:03

标签: javascript arrays

我来找你,因为我在使用javascript数组时遇到了一些问题。

我试图遍历一个充满了某些对象的javascript数组。 我总共有4个物体。

这是我的代码:

// sort trainings_tab descending order
trainings_tab = trainings_tab.sort(function(a, b) {
    if (a.date_from < b.date_from)
        return 1;
    else if(a.date_from > b.date_from)
        return -1;
    return 0;
});

console.log(trainings_tab);
for(var h = 0; h < trainings_tab.length; h++) {

    // var training = new_array[h];
    console.log(trainings_tab[h]);
}

以及我从console.log(trainings_tab)获得的内容; =&GT;

[] (i have a little blue i here that say : object value at left was snapshotted when logged value below was evaluated just now)
 0: Object
    _geoloc: Object
    _highlightResult: Object
    _rankingInfo: Object
    address: "Hospital da plastica, Rio de Janeiro, Brazil"
    address_id: 16623
    date_from: "1479769200"
    date_to: "1480028400"
    device: "USD"
    doctor_full_name: "John Doe"
    doctor_id: "85"
    doctor_image: "/images/avatar_85.jpg"
    doctor_url: "/profile/85"
    guest: "2"
    labo_id: "8So7KJPmv4qyL2uHD"
    langues: "English"
    objectID: "689"
    objectives: Array[2]
    price: "1000"
    procedures: Array[1]
    products: Array[1]
    seat_left: "2"
    seat_taken: 0
    speciality: "PLASTIC SURGERY, BODY CONTOURING"
    training_full: ""
    training_name: "Lipofacillomaxial"
    url: "/training/detail/689"
    __proto__: Object
 1: Object
 2: Object
 3: Object
 length: 4
 __proto__: Array[0]

每个对象看起来像第一个,其他数据为ofc。 但问题是我的for循环没有迭代其他数组,我无法看到console.log(trainings_tab [h]);

有人有想法可以帮助我PLZ吗?感谢。

编辑:我如何播放我的选项卡?

我在空标签处分配一个var:

var trainings_tab = [];

我使用ALGOLIA(https://www.algolia.com/)来搜索名为“培训”的索引。这得到了我的所有记录。当搜索完成后,我有一个回调,我这样做:

function searchDone(err, content) {
        if (err) {
            console.error(err);
            return;
        }
        //console.log(content.hits);
        for(var i = 0; i < content.hits.length; i++) {
            var training = content.hits[i];

            // check that's a training
            if (training && training.date_from != null) {
                // calculate  seats taken
                var seats_taken = training.guest - training.seat_left;

                var training_full = '';
                // training is full ?
                if (training.seat_left == 0) training_full = 'training-full';

                training.seat_taken = seats_taken;
                training.training_full = training_full;

                if (training.hidden == false || training.hidden == null)
                    Session.set("countTrainings", Session.get("countTrainings")+1);

                trainings_tab.push(training);
            }
        }

    }

似乎我的问题是当我填充trainings_tab时,因为如果我在排序之前使用console.log,我的显示方式与之前引用的显示相同...

0 个答案:

没有答案