JSFiddle - React, Tailwind, and code Playground

by Pratik Bhoir

HTML

<input type="checkbox" value="1" name="0609">
<input type="checkbox" value="2" name="0609">
<input type="checkbox" value="7" name="0609">
<input type="checkbox" value="1" name="0912">
<input type="checkbox" value="2" name="0912">
<input type="checkbox" value="7" name="0912">
<button onClick="getData()">Get Data</button>

JavaScript

function getData() {
    var obj = {}; // Define object
    // Loop over all checkboxes
    $(':checkbox').each(function () {
        var name = $(this).attr('name'); // Get name of this checkbox
        var dayIndex = $(this).val() - 1; //Get the dayIndex from value
        if (obj[name]) {
            obj[name][dayIndex] = 1; //set 1 to the value
        } else {
            obj[name] = [0, 0, 0, 0, 0, 0, 0]; // Create array and push value
            obj[name][dayIndex] = 1; //Set 1 to the array
        }
    });
    console.log(obj);
}