Bilal Bayasut
Bilal Bayasut

Reputation: 121

Efficient way to search inside list of json based on its key containment

I have some variables and JSON from api, let say

var airlines = "Airasia";
var hotel = "Hyatt";
var tourType = "holiday";
var result = JSON.parse(data);

the json structure is following:

    [ { id: 1,
        departure_month: 'September - November 2019',
        program: '9 Days',
        package_name: 'Example package 1',
        tour_type: 'holiday',
        airlines: 'Lion',
        hotel: 'Hyatt'
      },
      { id: 2,
        departure_month: 'September - November 2019',
        program: '15 Days',
        package_name: 'Example package 2',
        tour_type: 'pilgrimage',
        airlines: 'Saudia',
        hotel: 'pullman'
       },
      { id: 3,
        departure_month: 'September - November 2019',
        program: '10 Days',
        package_name: 'Example package 3',
        tour_type: 'holiday',
        airlines: 'Airasia',
        hotel: 'Hyatt'
       }
    ]

I want to efficiently search if one or more of the parameters (ex: airline, hotel, tourType) are exist or not and the code will return the json that match with those keys

so for example if alirlines = "Saudia" and hotel = "pullman" and the rest is empty, then the code will only return corresponding parameters

{ id: 2,
    departure_month: 'September - November 2019',
    program: '15 Days',
    package_name: 'Example package 2',
    tour_type: 'pilgrimage',
    airlines: 'Saudia',
    hotel: 'pullman'
   }

Upvotes: 0

Views: 51

Answers (2)

Junius L
Junius L

Reputation: 16142

create a function that takes array of keys, and use includes and filter to filter on those values.

const json = [ { id: 1,
        departure_month: 'September - November 2019',
        program: '9 Days',
        package_name: 'Example package 1',
        tour_type: 'holiday',
        airlines: 'Lion',
        hotel: 'Hyatt'
      },
      { id: 2,
        departure_month: 'September - November 2019',
        program: '15 Days',
        package_name: 'Example package 2',
        tour_type: 'pilgrimage',
        airlines: 'Saudia',
        hotel: 'pullman'
       },
      { id: 3,
        departure_month: 'September - November 2019',
        program: '10 Days',
        package_name: 'Example package 3',
        tour_type: 'holiday',
        airlines: 'Airasia',
        hotel: 'Hyatt'
       }
    ]
    
function searchJson(array, keys) {

  return array.filter(item => {
    const string = item && typeof item === 'object' && JSON.stringify(item).toLowerCase();

    return keys.filter(key => string && string.includes(key.toLowerCase())).length === keys.length;
  })
}

console.log(searchJson(json, ['Saudia', 'pullman']))

Upvotes: 1

dyouberg
dyouberg

Reputation: 2332

I think you just want the built in ES6 operator find which lets you specify your search parameters and will return the first element it matches in the array.

See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Example:

result.find(item => item.airlines === 'Saudia' && item.hotel === 'pullman');

If you want to find ALL matches then you would use result.filter instead of find.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Upvotes: 0

Related Questions