如何在foreach循环中获取jquery foreach中的值

时间:2017-06-22 20:06:37

标签: javascript jquery

我有一个包含一个数组的json响应,在该数组中我还有一个数组。

我的AJAX:

div

rentpriceRangeFilter.php(我返回json回复)

$.ajax({
  type: "POST",
  data: { city: city, priceRange: range },
  url: "rentpriceRangeFilter.php",
  success: function(data) {
    var res = jQuery.parseJSON(data); // convert the json
    console.log(res);
    if (res['status'] == 1) {
      var htmlString = '';
      $.each(res['data'], function(key, value) {
        console.log(key + ' is ' + value.house);
      });
    }
  },
});

我的JSON响应如下:

echo json_encode(array("status"=>"1","response"=>"success","data"=>$mainArray));

现在我想显示{ "status": "1", "response": "success", "data": [{ "id": "2", "house": "1 BHK Apartment for Lease", "rentLease_amount": "6000000", "furnished_type": "Semi-Furnished", "CreatedOn": null, "Rent": "60 Lac", "multipleImages": [{ "imageId": "3", "rentImageId": "2", "rentImageName": "2cf011438e6cd43c6bfaa6cac653d86e.jpg" }, { "imageId": "4", "rentImageId": "2", "rentImageName": "e2c5be420130370c5118120a1bc749c6.jpg" }] }, { "id": "1", "house": "1 BHK Aparatment for Marathahalli", "rentLease_amount": "500000", "furnished_type": "Fully-Furnished", "CreatedOn": null, "Rent": "5 Lac", "multipleImages": [{ "imageId": "1", "rentImageId": "1", "rentImageName": "7d2905c30ab211732b97dbf165c75526.jpg" }, { "imageId": "2", "rentImageId": "1", "rentImageName": "bcf6cb343aaa2cc8e50ff52baa062bcc.jpg" }] }] } 名称和house值,但在我的ajax页面中,我只能获得自己的名字,而不是如何显示multipleImages

2 个答案:

答案 0 :(得分:0)

您可以在multipleImages上使用另一个循环,如下所示:

$.ajax({
    type: "POST",
    data: {city:city,priceRange:range},
    url: "rentpriceRangeFilter.php",
    success:function(data){
        var res=jQuery.parseJSON(data);// convert the json
        console.log(res);

        if(res['status']==1){
            var htmlString='';
            $.each( res['data'], function( key, value ) {
                console.log(key + ' is ' + value.house);
                $.each( value.multipleImages, function( key2, image ) {             
                    console.log('imageId of ' + key2 + ' is ' + image.imageId);
                    console.log('rentImageId of ' + key2 + ' is ' + image.rentImageId);
                    console.log('rentImageName of ' + key2 + ' is ' + image.rentImageName);
                });
            });
        }
    },
});

答案 1 :(得分:0)

您可以再次浏览您的媒体资源:

这是一个展示解决方案的方法:https://jsfiddle.net/equbq6rf/

const data = [{
    "house":"My house",
    "multipleImages":[{
        "imageId":1,
        "rentImageId":2,
        "rentImageName":"image_name.jpg"
    },{
        "imageId":3,
        "rentImageId":4,
        "rentImageName":"image_name2.jpg"
    }]
},{
    "house":"My house number 3",
    "multipleImages":[{
        "imageId":5,
        "rentImageId":6,
        "rentImageName":"image_name3.jpg"
    },{
        "imageId":7,
        "rentImageId":8,
        "rentImageName":"image_name4.jpg"
    }]
}];

 $.each(data, (key, value) => {
   console.log(key + ' is ' + value.house);
   $.each(value['multipleImages'], (imageKey, imageValue) => {
    Object.keys(imageValue).forEach((property) => {
        console.log(imageValue[property]);
    });
   });
 });

你可以进一步推动但它应该可以解决问题。

希望有所帮助:)

迪伦