5
\$\begingroup\$

I am trying to create a filter string according to the fields available with the following code, how could i improve the following with a switch case or better way?

   if ((this.selectedCity === 'City') && this.selectedCategory === 'Category' && this.fromDate === '') {
            this.filter = `{}`;
        } else if ((this.selectedCity !== 'City') && this.selectedCategory === 'Category' && this.fromDate === '') {
            this.filter = `{"location.city":"${this.selectedCity}"}`;
        } else if ((this.selectedCity === 'City') && this.selectedCategory !== 'Category' && this.fromDate === '') {
            this.filter = `{"category":"${this.selectedCategory}"}`;
        } else if ((this.selectedCity === 'City') && this.selectedCategory !== 'Category' && this.fromDate !== '') {
            this.filter = `{"date.start":{"$gte":"${this.fromDate}"},"date.end":{"$lt":"${this.toDate}"}}`;
        } else if ((this.selectedCity !== 'City') && this.selectedCategory !== 'Category' && this.fromDate === '') {
            this.filter = `{"location.city":"${this.selectedCity}","category":"${this.selectedCategory}"}`;
        } else if ((this.selectedCity !== 'City') && this.selectedCategory === 'Category' && this.fromDate !== '') {
            this.filter = `{"location.city":"${this.selectedCity}","date.start":{"$gte":"${this.fromDate}"},"date.end":{"$lt":"${this.toDate}"}}`;
        } else if ((this.selectedCity === 'City') && this.selectedCategory === 'Category' && this.fromDate !== '') {
            this.filter = `{"date.start":{"$gte":"${this.fromDate}"},"date.end":{"$lt":"${this.toDate}"}}`;
        } else if ((this.selectedCity !== 'City') && this.selectedCategory !== 'Category' && this.fromDate !== '') {
            this.filter = `{"location.city":"${this.selectedCity}","category":"${this.selectedCategory}","date.start":{"$gte":"${this.fromDate}"},"date.end":{"$lt":"${this.toDate}"}}`;
        }
\$\endgroup\$
3
\$\begingroup\$

I recommend building the filter in steps, starting with an empty object {}. You then add attributes according to the selected city, category and date. And finally, you convert it to a JSON string via JSON.stringify(filter):

let filter = {};

if (this.selectedCity !== 'City') {
  filter.location = {
    city: this.selectedCity
  };
}

if (this.selectedCategory !== 'Category') {
  filter.category = this.selectedCategory;
}

if (this.fromDate !== '') {
  filter.date = {
    start: {'$gte': this.fromDate},
    end:   {'$lt':  this.toDate}
  };
}

this.filter = JSON.stringify(filter);
\$\endgroup\$
0
\$\begingroup\$

JSON.stringify ignores undefined values:

JSON.stringify( { a: 1, b: undefined } )  // '{"a":1}'

so it can be shortened to:

this.filter = JSON.stringify( {
  city    : this.selectedCity     == 'City'     ? void 0 : this.selectedCity,
  category: this.selectedCategory == 'Category' ? void 0 : this.selectedCategory,
  date    : this.fromDate         == ''         ? void 0 :
    { start: { '$gte': this.fromDate }, end: { '$lt': this.toDate }
} );
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.