Access our premium support and let us know your problems, we will help you solve them.

0
No products in the cart.

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: Convert form data into multi-dimensional array #9335
    blankreihandio
    Participant

    To transform the other fields into a nested object, you can modify the data preparation process. You’ll need to split the field names and structure the object accordingly. Here’s a function you can use:

    function transformFormData(formData) {
      const data = {};
    
      for (let [key, value] of formData.entries()) {
        if (key.startsWith('other[')) {
          const nestedKey = key.substring(key.indexOf('[') + 1, key.indexOf(']'));
          if (!data['other']) {
            data['other'] = {};
          }
          data['other'][nestedKey] = value;
        } else {
          data[key] = value;
        }
      }
    
      return data;
    }data;
    }
    

    Replace your current data preparation process with this function:

    const data = transformFormData(new FormData(this.form));
    

    This function iterates through the form data entries and checks if the key starts with ‘other’. If it does, it extracts the nested key (like ‘a’ or ‘b’) and structures the object accordingly. Otherwise, it stores the key-value pair directly in the main object.

Viewing 1 post (of 1 total)