Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:37:53+00:00 2026-05-25T23:37:53+00:00

I want append some from array by jQuery.each and data hotel_id , number each

  • 0

I want append some from array by jQuery.each and data hotel_id, number each hotel_id is 4, and this loop $.each(data[0].hotel_id,... }); run 4 times contents inside self, in case that there are or inserted in database rows residence_u and residence_p 3 times (each of they there are 3, not 4(like hotel_id)), when run the following code, this code not have error jus not work. if i remove codes $.each(info_ru.units,... }); and $.each(info_rp.start_date,... }); it worked:

(there are four data[0].hotel_id items but only three data[0].residence_u items. So when the outer loop gets to index === 3, there isn’t a data[0].residence_u[index])

See full my js code: http://jsfiddle.net/6sGBd/

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"]
    }]
}]

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

How can fix it and What do i do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:37:54+00:00Added an answer on May 25, 2026 at 11:37 pm

    The length of the residence_u array is only 3, so when index == 4 and you assign var info_ru = data[0].residence_u[index]; and then accessing an element of the undefined info_ru is an error and stops execution.

    You can fix it by checking that info_ru and info_rp are objects that contain the the element you are looking for.

    $.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];
        if (info_ru && typeof info_ru === 'object' && info_ru.hasOwnProperty('units')) {
            $.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];
        if (info_rp && typeof info_rp === 'object' && info_rp.hasOwnProperty('start_date')) {
            $.each(info_rp.start_date, function (index, value) {
                $tooltip.append(value + ' & ' + info_rp.end_date[index] + ' & ' + info_rp.price_change[index] + '<br>');
            });
        }
        tool_tip()
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display some data which comes from my db using jQuery.ajax() (i.e.
I'm trying to append some data to a file, but in some cases want
jQuery.get(ChkNewRspLive.php?lastmsgID= + n, function(newitems){ //some code to separate values of 2d array. $('#div1').append(msgid); $('#div2').append(rspid);
I want to append some additional html to the event divs depending on data
I want my application to download some data from the internet, in iPhone SDK
I want to append some nodes to an xml document using Linq2XML. The file
I want to pre-append some text a a CSV file that is created by
I am working on a utility. I want to append data at the top
I have a list like: list = [[1,2,3],[4,5,6],[7,8,9]] I want to append a number
I'm trying to increment through JQuery loop to fill out some HTML code with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.