title: Javascript dynamic checkbox values as array
body: <p>I currently have a json file that contains the inventory of vehicles.  I have a search page that allows the users to filter the inventory using checkboxes, such as make, model, color, etc..</p>
  <p>I need to pass the checkbox values as an array.  For example, if I select Mustang and Camaro checkboxes, it would need to be <code>'Model' =&gt; ['Mustang', 'Camaro']</code>.</p>
  <p>I tried to create 2 arrays.  One for the keys, (Make, Model, Color), etc... and another for each of the values, (Mustang and Camaro).  Some values may have one item, some may have several.  For example, if we wanted all red Mustang's and Camaro's, the array should look like:</p>
  <pre><code>['Model' =&gt; ['Mustang', 'Camaro'],Color' =&gt; ['Red']];
  </code></pre>
  <p>Or, just red Mustangs:</p>
  <pre><code>['Model' =&gt; ['Mustang'],Color' =&gt; ['Red']];
  </code></pre>
  <p>I can loop each of the checkboxes, and I attempted to put all the checkbox names (data.id) as a key, and a second array (data.value) as the values.  If the key exists, then add each of the values that have the same key.</p>
  <pre><code>var kv=[]; // Keys
  var vv=[]; // Array of the values for each key.
  
  $(&quot;[name='checkBoxItem']&quot;).each(function (index, data) {
     if (data.checked) {
            if(!(data.id in kv)){  // Is always called.
                 alert(&quot;Does't exist, adding : &quot; + data.value);
                 kv.push(data.id);
                 vv.push(data.value);
                    } else {
                      vv.push(data.value);
                    }
                checked.push({[kv]: vv});
              }
          });
  </code></pre>
  <p>My code returns:</p>
  <pre><code>Array ( [selected_values] =&gt; Array([0] =&gt; Array ([Make] =&gt; Array([0] =&gt; Mustang))))
  </code></pre>
  <p>Which is close, but <code>if(!(data.id in kv))</code> is always being called, preventing me from adding multiple selections.</p>
  <p>I am sorry if I am having coders block on something that I should have been able to do.</p>