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 8786041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:30:47+00:00 2026-06-13T21:30:47+00:00

I wonder if I can simplify and use less lines of code for this

  • 0

I wonder if I can simplify and use less lines of code for this purpose:

I have a class called “worker”, and that class has a method that reads the properties (name, age, etc…) from a series of simple arrays.

Until there, everything is fine. Now, one of the properties that I want to add is a boolean value that makes reference to which months of the year the worker is active. For the moment, I have solved it like this:

var months_worker_1 = [{"jan":true},{"feb":true},{"mar":true},{"apr":false}] //and so on

And then, my property reads months_worker_1, but I have one array like that for each worker. I wonder if there is a way to do this that requires less lines of code, like for example, create a “master” array with all the months of the year, and in the array for each worker, specify just the months they are working. Those months become “true”, and the rest of months become “false” automatically without specifying so… I have been scratching my head for some time, and for the moment only my current system is working fine, but I am guessing that there must be a simpler way…

Thanks very much!

Edit: I clarify, there is no “big picture”. I am just doing some exercises trying to learn javascript and this one woke my interest, because the solution I thought seems too complicated (repeating same array many times). There is no specific goal I need to achieve, I am just learning ways to do this.

  • 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-13T21:30:49+00:00Added an answer on June 13, 2026 at 9:30 pm

    A really nice trick that I use sometimes is to use a binary number to keep track of a fixed amount of flags, and convert it to a decimal for easier storage / URL embedding / etc. Let’s assume Mark, a user, is active all months of the year. Considering a binary number, in which 1 means “active” and 0 inactive, Mark’s flag would be:

    111111111111 (twelve months)

    if Mark would only be active during january, february and december, his flag value would be:

    11000000001

    Checking if Mark is active during a specific months is as simple as checking if the character that corresponds to that month’s index in Mark’s flag is 1 or 0.

    This technique has helped me in the past to send values for a large number of flags via URLs, while also keeping the URL reasonably short. Of course, you probably don’t need this, but it’s a nice thing to know:

    Converting from binary to decimal is easy in JS:

    parseInt(11000000001, 2).toString(10); // returns 1537
    

    And the reverse:

    parseInt((1537).toString(2)); // returns 11000000001
    

    Edit

    You could just as easily use an array made out of the month numbers:

    var months_worker_1 = [1, 2, 3]; // this would mean that the user is active during january, february and march
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder how can I change my code so, that the number of given
I wonder how can I code this: //if there is any ajax request $("#loader").css("display","block");
I have something like this $.ajaxSetup({ cache: false }); I am wonder can you
I'm making many graphs which have same xlab. I wonder can we use xlab
I wonder that can I avoid NullPointerException in _log.info. I have 1 null object
wonder whether someone can help me with the following one... I have a struct
I wonder about that can I write native SQL to add or delete operations
I wonder why I can't read the JSON Object like this : { 1:{bulan:Januari,tahun:2012,tagihan:205000,status:Lunas},
this question is uncategorized.. i guess. I wonder can i convert every recorded voice
Im using Jquery Post in my code, and Im wonder how can I call

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.