gpt4 book ai didi

javascript - 从对象数组中获取 JavaScript 对象

转载 作者:行者123 更新时间:2023-11-28 17:52:23 26 4
gpt4 key购买 nike

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

this return array of obejcts

var arrProducts = @Model.ViewModel_SessionObject.GetJsonProducts();
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}];

有没有一种方法可以在我单击 anchor 标记时获取该对象,该 View 是我的所有对象列表的 View ,更清楚地说,当我单击 anchor 标记时,我想通过 RoomCode 和 RateCode 选择或查找对象。

@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>
}
}

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

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

};
</script>

最佳答案

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

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

从哪里获取值(上例中的 IBEA1D)取决于您

关于javascript - 从对象数组中获取 JavaScript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45213404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com