开发者

Initialise a counter before the first loop

开发者 https://www.devze.com 2023-04-09 05:25 出处:网络
This line $.each(data[0].hotel_id, function (index, value) { Loops through hotel_id array executing the inner JS code for each entry in the dictionary setting index to the key in the dictionary and

This line

$.each(data[0].hotel_id, function (index, value) {

Loops through hotel_id array executing the inner JS code for each entry in the dictionary setting index to the key in the dictionary and value to the contents.

My dictionary:

"hotel_id": [{
    "hotel_id": ["1"]
}, {
    "hotel_id": ["2"]
}, {
    "hotel_i开发者_开发技巧d": ["3"]
}, {
    "hotel_id": ["4"]
}],

Has all items with the same index, therefore the value of index will always be 'hotel_id', which is no use to i when i try to use it to find the relevant extra data from my other dictionaries.

I would initialise a counter before the first loop and increment it on each iteration then use this value to find the associated extra data from the other arrays.

See full my js code: http://pastebin.com/jBWEDZrN

This is summary from my js code in ajax call(url: 'get_gr',):

$.each(data[0].hotel_id, function (index, value) {
    var $li = $('<li><input name="hotel_id[]" value="' + value.hotel_id + '" style="display:none;"></li>');
    var $tooltip = $('<div class="tooltip"></div>').appendTo($li);
    $li.appendTo('#residence_name');

    var info_ru = data[0].residence_u[index];
    $.each(info_ru.units, function (index, value) {
        $tooltip.append(value + ' & ' + info_ru.extra[index] + ' & ' + info_ru.price[index] + '<br>');
    });

    var info_rp = data[0].residence_p[index];
    $.each(info_rp.start_date, function (index, value) {
        $tooltip.append(value + ' & ' + info_rp.end_date[index] + ' & ' + info_rp.price_change[index] + '<br>');
    });
    tool_tip()
});

This is output php code(url: 'get_gr',):

[{
    "guide": null,
    "hotel_id": [{
        "hotel_id": ["1"]
    }, {
        "hotel_id": ["2"]
    }, {
        "hotel_id": ["3"]
    }, {
        "hotel_id": ["4"]
    }],
    "residence_u": [{
        "units": ["hello", "how", "what"],
        "extra": ["11", "22", "33"],
        "price": ["1,111,111", "2,222,222", "3,333,333"]
    }, {
        "units": ["fine"],
        "extra": ["44"],
        "price": ["4,444,444"]
    }, {
        "units": ["thanks", "good"],
        "extra": ["55", "66"],
        "price": ["5,555,555", "6,666,666"]
    }],
    "residence_p": [{
        "start_date": ["1111", "2222"],
        "end_date": ["1111", "2222"],
        "price_change": ["1111", "2222"]
    }, {
        "start_date": ["3333", "444"],
        "end_date": ["3333", "4444"],
        "price_change": ["3333", "4444"]
    }, {
        "start_date": ["5555", "6666"],
        "end_date": ["5555", "6666"],
        "price_change": ["5555", "6666"]
    }]
}]

What do i do?

Update:(this update is for above output php)

I insert several(array) value with json_encode in one row from database table, now want echo they as order with jquery.

Output js code is this:

1

hello & 11 & 1,111,111

how & 22 & 2,222,222

what & 33 & 3,333,333,

1111 & 1111 & 1111

2222 & 2222 & 2222

2

fine & 44 & 4,444,444

3333 & 3333 & 3333

4444 & 4444 & 4444

3

thanks & 55 & 5,555,555

good & 66 & 6,666,666

5555 & 5555 & 5555

6666 & 6666 & 6666

4


No, your array doesn't have items with the same index. It has items with different indexes, but the value is an object that contains a property with the same name for each object. The index will go from 0 to 3 if the array has four items as in the example.

You can use value.hotel_id in the loop to get the array that is the value of the property, for example ["1"].

If the array in the property only ever contains one id, you can just use value.hotel_id[0] to get that id.

Example:

$.each(data[0].hotel_id, function (index, value) {
  alert(value.hotel_id[0]);
});

http://jsfiddle.net/Guffa/BWLqw/


I don't see the point of using each and an anonymous function.

I would just use a for loop.

for(var n = 0; n < data[0].hotel_id.length; n++) {
  // put your function code here or call a named func(data[0].hotel_id[n])
}

I only use each if I am applying a function that I would reuse elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消