简体   繁体   中英

Filtering JSON Array

Any guidance or reference is appreciated.

Using javascript's (map reduce and filter), I am trying to get a collection of all available rooms across multiple vendors in the given JSON array. The logic for this:

**User Input **: checkIn Date, checkOut Date and Property Name

**Filter Logic **

  • Check is the item availableFrom and availableTo dates fall in the range of checkIn and checkOut.
  • Check if the item propertyName matches the user input
  • Iterate through the items to get the common rooms

I have a JSON structure is:

var callOutResult = 
[
  {
    "vendorId": "1",
    "vendorName": "ABC",
    "vendorPropertyId": "100",
    "propertyName": "Some Name",
    "availableFrom": "2012-04-01T15:00:00.001Z",
    "availableTo": "2012-04-25T12:00:00.001Z",
    "available": {
      "floor1": ["101","105","106"],
      "floor2": ["202","204","208"],
      "floor3": ["301","304","307"],
      "floor4": ["405","409","410"],
      "floor5": ["502","504","506"],
      "floor6": ["602","607","609"]
    }
  },
  {
    "vendorId": "2",
    "vendorName": "XYZ",
    "vendorPropertyId": "300",
    "propertyName": "Some Name",
    "availableFrom": "2012-04-10T15:00:00.001Z",
    "availableTo": "2012-05-20T12:00:00.001Z",
    "available": {
      "floor1": ["104","106","107"],
      "floor2": ["208","214"]
    }
  },
  {
    "vendorId": "3",
    "vendorName": "PQR",
    "vendorPropertyId": "450",
    "propertyName": "Some Name",
    "availableFrom": "2012-01-23T15:00:00.001Z",
    "availableTo": "2012-06-25T12:00:00.001Z",
    "available": {
      "floor1": ["101","102","103"],
      "floor2": ["208","212"],
      "floor3": ["302","306","315"],
      "floor4": ["415","409","420"]
    }
  }
]

Code I have thus far


var checkInDate = new Date("2012-04-10");
var checkOutDate = new Date("2012-04-22");
var propertyName = 'Some Name'

// Check in & Check Out Date Filter
var filter1 = callOutResult.filter(function (arrayItem) {return arrayItem.availableFrom <= checkOutDate && arrayItem.availableTo >= checkInDate});

// Property Name Filter
var filter2 = filter1.filter(function (arrayItem) {return arrayItem.propertyName === propertyName});


result = Object
        .values(filter2)
        .reduce(function(a, b){
// CANNOT FIGURE OUT THE LOGIC TO ITERATE THROUGH THE rooms.
});

My goal is to filter the array to find available rooms. Reference to article, other sample code, anything that will guide me.

If you were trying to find the room(s) that fall within the specified checkin and checkout dates, have the required propertyName and are found in all the matched items, then the following might be helpful:

 var callOutResult = [ { "vendorId": "1", "vendorName": "ABC", "vendorPropertyId": "100", "propertyName": "Some Name", "availableFrom": "2012-04-01T15:00:00.001Z", "availableTo": "2012-04-25T12:00:00.001Z", "available": { "floor1": ["101","105","106"], "floor2": ["202","204","208"], "floor3": ["301","304","307"], "floor4": ["405","409","410"], "floor5": ["502","504","506"], "floor6": ["602","607","609"] } }, { "vendorId": "2", "vendorName": "XYZ", "vendorPropertyId": "300", "propertyName": "Some Name", "availableFrom": "2012-04-10T15:00:00.001Z", "availableTo": "2012-05-20T12:00:00.001Z", "available": { "floor1": ["104","106","107"], "floor2": ["208","214"] } }, { "vendorId": "3", "vendorName": "PQR", "vendorPropertyId": "450", "propertyName": "Some Name", "availableFrom": "2012-01-23T15:00:00.001Z", "availableTo": "2012-06-25T12:00:00.001Z", "available": { "floor1": ["101","102","103"], "floor2": ["208","212"], "floor3": ["302","306","315"], "floor4": ["415","409","420"] } } ]; // convert date strings into Date objects: callOutResult.forEach(e=>["From","To"].forEach(ft=>e["available"+ft]=new Date(e["available"+ft]))); var checkInDate = new Date("2012-04-10T15:10:00.001Z"); var checkOutDate = new Date("2012-04-22T09:00:00.001Z"); var propertyName = 'Some Name'; // check in & check out date and propertyName filter: var filter = callOutResult.filter(function(itm) { return itm.availableFrom <= checkInDate && itm.availableTo >= checkOutDate && itm.propertyName === propertyName}); // check the existence of the room(s) for all matched vendors: console.log(filter.map(e=>Object.values(e.available).flat(2)) // flatten rooms for each vendor.reduce((a,c)=>{return a.filter(e=>c.some(f=>e==f))})) // a room must occur in each vendor object!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM