0

AJAX result:

enter image description here

When I'm going to display this data in HTML other information displaying correctl, but when trying to display all_images and all_amenties data by using .each in same loop its showing [object] [object]. Below is my code:

"<div id='Carousel"+ this.product_id +"' class='carousel slide' style='width: auto; margin: 0 auto'>"+
      "<div class='carousel-inner'>"+
        $.each(this.all_images, function(k,v) {
            "<div class='item'>"+
                " <img src='"+ v +"' alt=''>"+
            "</div>"
        }) +
      "</div> "+
      "<a class='left carousel-control' href='#Carousel"+ this.product_id +"' data-slide='prev'>‹</a>"+
      "<a class='right carousel-control' href='#Carousel"+ this.product_id +"' data-slide='next'>›</a>"+
    "</div>"+
"</div>"+
1
  • 1
    please provide more details (like the full javascript function) and more description. Commented Feb 2, 2017 at 8:19

2 Answers 2

1

You can't concatenate an $.each call to a string. The result is an object - hence what you see in the output, [Object object].

To fix this you can generate the strings separately and concatenate them:

var html = this.all_images.map(function(src) {
  return '<div class="item"><img src="' + src + '" alt=""></div>';
}).join('');

html = '<div id="Carousel' + this.product_id + '" class="carousel slide" style="width: auto; margin: 0 auto"><div class="carousel-inner">' + html + '</div><a class="left carousel-control" href="#Carousel' + this.product_id + '" data-slide="prev">‹</a><a class="right carousel-control" href="#Carousel' + this.product_id + '" data-slide="next">›</a></div>';
Sign up to request clarification or add additional context in comments.

Comments

0

Simply change $.each to $.map, add a return before "<div class='item'>"+ and add .join('') after $.map(). $.map calls the function for each value in the array, stores the return value of each function in a new array, and returns it. So:

$.map([1, 2, 3], function (v) { return v * 2 }).join('') === '246'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.