Note that there are some explanatory texts on larger screens.

plurals
  1. POWays to simplify an array of objects that is repeated several times
    text
    copied!<p>I wonder if I can simplify and use less lines of code for this purpose: </p> <p>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. </p> <p>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: </p> <pre><code>var months_worker_1 = [{"jan":true},{"feb":true},{"mar":true},{"apr":false}] //and so on </code></pre> <p>And then, my property reads <code>months_worker_1</code>, 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...</p> <p>Thanks very much!</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload