Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulate form's data result

I would like to change the result of my form's data before submitting but I am struggling with my script

My HTML

<form>
    <table>
        <tr class="row_1">
            <td>
                <input name="flavour_1" type="text"><br>
                <input name="flavour_2" type="text">
            </td>
            <td>
                <input name="taste_1" type="text"><br>
                <input name="taste_2" type="text">
            </td>
        </tr>
        <tr class="row_2">
            <td>
                <input name="flavour_1" type="text"><br>
                <input name="flavour_2" type="text">
            </td>
            <td>
                <input name="taste_1" type="text"><br>
                <input name="taste_2" type="text">
            </td>
        </tr>
    </table>
    <button type="button" id="serForm">Serialize Form </button>
</form>

Javascript:

$("#serForm").on('click', function() {

    var db = $("form").serializeArray();

    for (var m = 0; m < db.length; m++) {
        var e = db[m],
            name = e.name,
            value = e.value;
        var parts = name.split(/_(?=\d+$)/);
        if (parts.length === 2) {
            name[parts[0]] = name[parts[0]] || [];
            name[parts[0]][parts[1] - 1] = e;
            delete e;
        }
    }
})

I am trying to get a result like or similar to this.

"flavour": [{"row_1_flavour_1_value", "row_1_flavour_2_value"},{"row_2_flavour_1_value", "row_2_flavour_2_value"}],
"taste": [{"row_1_taste_1_value", "row_1_taste_2_value"},{"row_2_taste_1_value", "row__taste_2_value"}]

Note

There is a possibility of having multiple rows (not only 2) and they are not named row_n the name is just for example purpose

like image 213
Jonathan Avatar asked Oct 29 '22 03:10

Jonathan


2 Answers

You can combine the use of Array.prototype.filter(), Array.prototype.map(), RegExp.prototype.test() and finally Array.prototype.reduce().

Code:

$('#serForm').on('click', function () {
  var db = $('form').serializeArray(),
      getFormRowValues = (arr, str, numPerRow) => arr
        .filter(el => new RegExp(str).test(el.name))
        .map(el => el.value)
        .reduce((a, c, i) => ((a[i / numPerRow | 0] = a[i / numPerRow | 0] || []).push(c), a), []),
      result = {
        flavour: getFormRowValues(db, 'flavour', 2),
        taste: getFormRowValues(db, 'taste', 2)
      };

  console.log(result);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <table>
    <tr class="row_1">
      <td>
        <input name="flavour_1" value="flavour_1_value" type="text"><br>
        <input name="flavour_2" value="flavour_2_value" type="text">
      </td>
      <td>
        <input name="taste_1" value="taste_1_value" type="text"><br>
        <input name="taste_2" value="taste_2_value" type="text">
      </td>
    </tr>
    <tr class="row_2">
      <td>
        <input name="flavour_1" value="flavour_1_value" type="text"><br>
        <input name="flavour_2" value="flavour_2_value" type="text">
      </td>
      <td>
        <input name="taste_1" value="taste_1_value" type="text"><br>
        <input name="taste_2" value="taste_2_value" type="text">
      </td>
    </tr>
  </table>
  <button type="button" id="serForm">Serialize Form </button>
</form>
like image 77
Yosvel Quintero Arguelles Avatar answered Nov 15 '22 06:11

Yosvel Quintero Arguelles


This solutions isn't as index heavy but I think it a bit more simple.

$("#serForm").on('click', function() {
    var flavourArr = getInputValuesByName('flavour');
    var tasteArr = getInputValuesByName('taste'); 

    console.log(flavourArr);
    console.log(tasteArr);
})

function getInputValuesByName(name){
    var arr = [];
    $('form input[name*="' + name + '"]').each(function(k,v){ 
        var parentClass = $(v).closest('tr').attr('class');
        
        arr.push(parentClass + "_" + $(v).val()); 
    });

    return arr;
}
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <body>
        <form>
        <table>
            <tr class="row_1">
            <td>
                <input name="flavour_1" value="flavour_1_value" type="text"><br>
                <input name="flavour_2" value="flavour_2_value" type="text">
            </td>
            <td>
                <input name="taste_1" value="taste_1_value" type="text"><br>
                <input name="taste_2" value="taste_2_value" type="text">
            </td>
            </tr>
            <tr class="row_2">
            <td>
                <input name="flavour_1" value="flavour_1_value" type="text"><br>
                <input name="flavour_2" value="flavour_2_value" type="text">
            </td>
            <td>
                <input name="taste_1" value="taste_1_value" type="text"><br>
                <input name="taste_2" value="taste_2_value" type="text">
            </td>
            </tr>
        </table>
        <button type="button" id="serForm">Serialize Form </button>
        </form>
    </body>
</html>
like image 20
big nol Avatar answered Nov 15 '22 06:11

big nol