Search code examples
sveltesveltekit

How to receive array of object in Sveltekit Action?


I'm new to Sveltekit,

I have a form with the input names set like so:

input 1: config[0][name]
input 2: config[0][type]
input 3: config[1][name]
input 4: config[2][type]

here in the Sveltekit Action I tried to console.log it:

'config[0][name]': '',
'config[0][type]': '',
'config[1][name]': '',
'config[1][type]': '',

so because of that I can only access the data like this formData['config[0][name]']

here's my code:

 const create: Action = async ({request})=> {
    const formData = Object.fromEntries(await request.formData());
    
    console.log("data", formData);
    }

how can I receive it as an array that I can access normally like this?

formData.config[0].name

Solution

  • To my knowledge there is currently no built-in way and a feature request for that has been rejected.

    The data essentially needs to be parsed based on this bracket notation which is just a common convention. There might be some existing libraries you could use or you can implement the logic yourself.