HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Make JSON based on available fields

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
availablefieldsmakebasedjson

Problem

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}"}}`;
        }

Solution

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);

Code Snippets

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);

Context

StackExchange Code Review Q#162427, answer score: 3

Revisions (0)

No revisions yet.