爵士
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" }
];
这可能,有人可以帮助我吗?
答案 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 ));