从对象数组中获取JavaScript对象

时间:2017-07-20 11:28:13

标签: javascript jquery arrays

我有一个包含四个对象的数组:

  

这个返回的obejcts数组

var arrProducts = [
{"RateCode":"IBE","RoomCode":"A1D","IDSharedAvailability":0,"TotalRooms":"4 rooms available","SelectedRooms":0},
{"RateCode":"PR2","RoomCode":"A1D","IDSharedAvailability":0,"TotalRooms":"4 rooms available","SelectedRooms":0},
{"RateCode":"IBE","RoomCode":"B2T","IDSharedAvailability":0,"TotalRooms":"7 rooms available","SelectedRooms":0},
{"RateCode":"PR2","RoomCode":"B2T","IDSharedAvailability":0,"TotalRooms":"7 rooms available","SelectedRooms":0}];
@foreach (var item in Model.ViewModel_SessionObject.HotelAvailabilityRSObject.RoomTypes)  // rooms Loop
{
    HotelDetailsRSRoomType RoomDetails = Model.ViewModel_SessionObject.HotelDetailsRSObject.GetRoomTypeByCode(item.RoomCode);
    if (RoomDetails != null)
    {
        <div style="border-left: 1px solid #fff;border-right: 1px solid #fff;padding: 10px;border-bottom:  1px solid #fff;background-color: #fff; ">

            <!--Rate describtion-->
            <br />
            <!--Rooms section-->
            <div id="rooms">
                <div class="row">
                    <div class="col-xs-12 col-sm-12 col-md-4 col-lg-3">
                        <!--rooms Name-->
                        <label>@RoomDetails.RoomName</label><br />
                        <p>
                            <!--rooms Image-->
                            <img height="200" width="200" style="border:solid;" src=@RoomDetails.GetRoomURL() />
                        </p>
                    </div>

                    <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 font">
                        <!--rooms Describtion-->
                        <div class="comment">
                            <label>@RoomDetails.RoomDescription</label><br />
                        </div>
                    </div>

                    <div class="col-xs-12 col-sm-12 col-md-4 col-lg-5">
                        @foreach (var rateitem in item.RateCodes)
                        {
                            <div style="background-color:#F9F9F9;border-bottom: 2px #c9c5c5 solid;">
                                <!--rooms Price-->
                                <label>@Model.ViewModel_SessionObject.HotelDetailsRSObject.GetRateCodeByCode(rateitem.RateCode).RateName</label><br />
                                <div class="tooltip" style="margin-left: 83px;">
                                    <img src="~/img/info.png" alt="Alternate Text" />
                                    <span class="tooltiptext">@Model.ViewModel_SessionObject.HotelDetailsRSObject.GetRateCodeByCode(rateitem.RateCode).RateDescription</span>
                                </div>
                                <div class="tooltip" style="margin-left: 42px;">
                                    @if (rateitem.CancellationPolicies[0].CancelTimestamp > DateTime.Today)
                                    {
                                        <img src="~/img/FreeCancellationPossible.png" alt="Alternate Text" />
                                        <span class="tooltiptext">@rateitem.CancellationPolicies[0].GetCancellationPolicyText()</span>
                                    }
                                    else
                                    {
                                        <img src="~/img/FreeCancellationNotPossible.png" alt="Alternate Text" />
                                        <span class="tooltiptext">@rateitem.CancellationPolicies[0].GetCancellationPolicyText()</span>
                                    }
                                </div>
                                <div class="tooltip">

                                    @if (rateitem.MealsIncluded.ToString() == "NoMeals")
                                    {
                                        <img src="~/img/NoMealsIncl.png" alt="Alternate Text" />
                                        <span class="tooltiptext">@rateitem.GetMealsText()</span>
                                    }
                                    else
                                    {
                                        <img src="~/img/MealsIncl.png" alt="Alternate Text" />
                                        <span class="tooltiptext">@rateitem.GetMealsText()</span>
                                    }
                                </div><br /><br /><br />
                                <label>@rateitem.GetAvailabilityText()</label>
                                <div id="btn_select" style='position:relative;background-color:#77d711;width:89px;height:40px;float: right;bottom: 67px;'>
                                    <p style='display:inline;color:#ffffff;float:right;margin-top: 10px;margin-right: 4px;'>
                                        @rateitem.TotalRate.ToString("N2")
                                        @Model.ViewModel_SessionObject.HotelAvailabilityRSObject.CurrencyCode
                                    </p>
// This is the anchor tag i want click on to select the object he has            <a style='position:absolute;top:0px;left:0px;width:100%;height:100%;display:inline;' href='javascript:SelectRooms(arrProducts);'></a>
                                </div><br /><br /><br />
                                <div id="btn_remove" style='position:relative;background-color:#11d7c9;width:89px;height:30px;float: right;display:none;'>
                                    <p style='display:inline;color:#ffffff;float:right;margin-top: 5px;margin-right: 20px;'>
                                        Remove
                                    </p>
                                    <a style='position:absolute;top:0px;left:0px;width:100%;height:100%;display:inline;' href='#'></a>
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>
            <!--End rooms section-->
        </div>
    }
}

当我点击锚标记这个我所有对象列表的视图时,有没有办法可以获取对象, 更清楚我想在我点击锚标签时通过RoomCode和RateCode选择或找到对象。

<script type="text/javascript">
    function SelectRooms(arrProducts) {
        var filterObj = arrProducts.filter(function (e) {
            return e.
        });

     };
</script>

我尝试了这个功能,但是当我点击锚标签时它会给出或返回所有对象,但是我需要锚点选择的特定对象:

{{1}}

2 个答案:

答案 0 :(得分:2)

传递给filter的方法必须为您要返回的任何项目返回真值。

function SelectRooms(arrProducts) {
    var filterObj = arrProducts.filter(function (e) {
        return e.RateCode === "IBE" && e.RoomCode === "A1D";
    });
    return filterObj[0]; // first/single item
 };

从上面示例中的{IBEA1D获取值的位置取决于您

答案 1 :(得分:1)

我不是100%确定你想要什么,但我猜你的错误是不通过房间代码/房间而不是你的功能。 如果是这种情况,您可能会觉得这很有用:

通过“RoomCode”作为单个对象获得空间:

function getRoom(arrProducts, code){
    //for modern browsers (> ie 11)
    return arrProducts.find(x => x.RoomCode === code);

    //for a bit older ones
    return arrProducts.filter(function(x) {
        return x.RoomCode === code;
    }).pop();

    //for really old ones
    for(var i = arrProducts.length; i--;;){
        if (arrProducts[i].RoomCode === code) return arrProducts[i];
    }

    return null;
}

将“RoomRate”作为数组获取房间:

function getarrProducts(arrProducts, rate) {
    //for modern browsers (> ie 11)
    return arrProducts.filter(x => x.RoomRate === rate);

    //for a bit older ones
    return arrProducts.filter(function(x) {
        return x.RoomRate === rate;
    });

    //for really old ones
    var results = [];
    for(var i = arrProducts.length; i--;;){
        if (arrProducts[i].RoomRate === rate) results.push(arrProducts[i]);
    }
    return results;
}

更新

如果您需要“RoomCode”和“RoomRate”的房间,您可以这样做:

function getRoom(arrProducts, code, rate) {
    //for modern browsers (> ie 11)
    return arrProducts.find(x => x.RoomCode === code && x.RoomRate === rate);

    //for a bit older ones
    return arrProducts.filter(function(x) {
        return x.RoomCode === code && x.RoomRate === rate;
    }).pop();

    //for really old ones
    for(var i = arrProducts.length; i--;;){
        if (arrProducts[i].RoomCode === code && x.RoomRate === rate) return arrProducts[i];
    }

    return null;
}

文档: