Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:41:56+00:00 2026-06-06T03:41:56+00:00

I currently create an object on page load with all possible key/value pairs and

  • 0

I currently create an object on page load with all possible key/value pairs and use true or false to determine their state. It would be more efficient if only items that were true were present though.

Sample data:

    filter =  {
            "country": 1,
            "Age Group": {
                2: false,
                3: true,
                4: false,
                5: false,
                6: false,
                7: false
            },
            "Gender": {
                1: false,
                2: false
            },
            "NEWSEC": {
                1: false,
                2: true,
                3: false,
                4: false,
                5: false
            },
            "Telco_Segment": {
                1: true,
                2: true,
                3: true,
                4: false,
                5: false,
                6: false
            }
    };

function:

function facetBuilder(key, val)
{
    if(key == 'country')
    {   
        filter.country = val;
    }
    else
    {
        if( filter[key][val] == true )
        {
            filter[key][val] = false;
        }
        else
        {
            filter[key][val] = true;
        }
    }
    console.log(filter);
}

Problem:

The function I use to apply these filters loops through them, so it would be more beneficial if I only had the objects such as “Age Group” present if it contained any values. In fact, it would be nice to have something like the following:

    filter =  {
            "country": 1,
            "Age Group": [3],
            "NEWSEC": [2],
            "Telco_Segment": [1,2,3]
    };

calling facetBuilder would essentially add/remove an object based on the presence of value(s) for that object.

I’m drawing a blank on how to approach this one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:41:57+00:00Added an answer on June 6, 2026 at 3:41 am

    You don’t need to explicitly check for true. You can check for the existence for an entry by doing something like this:

    if(map[key]) {
      ...
      ...
    }
    

    However if the value associated with that key is 0, then this is a problem, so it’s better to do:

    if(typeof map[key] !== "undefined") {
       ...
       ...
    }
    

    The reason you want to use typeof in this case is that a direct comparison against undefined can be problematic due to the possibility of undefined being overwritten by other code:

    window.undefined = "not undefined";
    "not undefined" == "undefined" //returns a true value
    

    Then your code would look like this:

    if(typeof filter[key][val] !== "undefined" )
    {
        delete filter[key][val]; //I'm assuming you want to remove it
    }
    else
    {
        filter[key][val] = true;
    }
    

    Another thing, when checking for a true value, it’s enough to say

    if([expression that evaluates to a boolean value]) {
        ...
    }
    

    You don’t have to do:

    if([expression that evaluates to a boolean value] == true) {
        ...
    }
    

    or

    if([expression that evaluates to a boolean value] == false) {
        ...
    }
    

    Another way to check for the existence of a property is to use the in operator:

    var stuff = {
       a: 10,
       c: 20,
       d: 30
    };
    
    var ay = "a"
    console.log(ay in blab, "b" in blab, "c" in blab, "d" in blab);
    >>true false true true
    

    I’m assuming this is what you want to do. If not, please let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this action to create an object. def create @campaign = Campaign.create(params[:campaign])
Currently I have this code var App = Ember.Application.create(); App.user = Ember.Object.create({ people: customers
I'm currently measuring the time spent to load a web page from a C#
We're currently using WPF to create a multi-page invoice document, to then be printed
My MainPage.xaml is a pivot page with 3 PivotItems. Currently it is loading all
I'm currently working on a C# program that creates a List, of object Task,
How to create datetime object representing the very last moment of the current month
I want to create a global object that represents the current user in Code
I'd like to create a UIImage object from the current graphics context. More specifically,
currently i create dynamic database using CreateDatabase() method in Entity Framework. it executes sucessfully

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.