Asp.net Jquery数组搜索

时间:2014-10-30 05:03:32

标签: jquery asp.net

爵士

var reservations = [
    { "HotelId": "01", "HotelName": "SPA", "ReservNum": "0166977", "Guest Name": "Jonny" },
    { "HotelId": "02", "HotelName": "DPA", "ReservNum": "0166977", "Guest Name": "Jonny" },
    { "HotelId": "03", "HotelName": "BPA", "ReservNum": "0166977", "Guest Name": "Jonny" },
    { "HotelId": "01", "HotelName": "SPA", "ReservNum": "0166910", "Guest Name": "Jomon" },
    { "HotelId": "02", "HotelName": "DPA", "ReservNum": "0166977", "Guest Name": "Jonny" }       
];

这是我的数组,我想搜索"HotelId": "01",我想得到这个数组

var newArray = [
    { "HotelId": "01", "HotelName": "SPA", "ReservNum": "0166977", "Guest Name": "Jonny" }, 
    { "HotelId": "01", "HotelName": "SPA", "ReservNum": "0166910", "Guest Name": "Jomon" }
];

这可能,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

试试这个: -

var reservations = [ { "HotelId": "01", "HotelName": "SPA", "ReservNum": "0166977", "Guest Name": "Jonny" }, { "HotelId": "02", "HotelName": "DPA", "ReservNum": "0166977", "Guest Name": "Jonny" }, { "HotelId": "03", "HotelName": "BPA", "ReservNum": "0166977", "Guest Name": "Jonny" }, { "HotelId": "01", "HotelName": "SPA", "ReservNum": "0166910", "Guest Name": "Jomon" }, { "HotelId": "02", "HotelName": "DPA", "ReservNum": "0166977", "Guest Name": "Jonny" }
]

function getByHotelId(id)
{
  return $.grep(reservations,function(d,i){
    return d.HotelId==id
  });
}
var newArray =getByHotelId('01');
alert(JSON.stringify(newArray ));

Demo