albert
albert

Reputation: 1816

How to parse array data from form submission with nodejs?

I have a form that aggroup the data on classes class_a[name], class_a[description] , class_b[name], class_b[description], etc. On nodejs, using body parser, the output of req.body is

class_a[name]: "name of class a"
class_a[description] : "description of class a"
class_b[name]: "name of class b"
class_b[description] : "description of class b"

and so on.

But i cant get as an object, i.e. i cant do get the value like "class_a.name" or "class_b.description". How to do this ?

Upvotes: 0

Views: 170

Answers (4)

Niraj Patel
Niraj Patel

Reputation: 810

You can map data in frontend in the following format, I guess that would be easy for you to iterate on it on node JS side:

const classData = [{
 className: "a",
 name: "name of class a",
 description: "description of class a"
},
{
 className: "b",
 name: "name of class b",
 description: "description of class b"
},
{
 className: "a",
 name: "name1 of class a",
 description: "description1 of class a"
}];

For Eg: If you want to get data of className "a" from the above data, then you can get in the following way:

const dataWithClassA = classData.filter((singleClass) => {return singleClass.className === "a"});

Upvotes: 1

AlpeshVasani
AlpeshVasani

Reputation: 227

I think this is what you need.

class_a["name"]: "name of class a"
class_a["description"] : "description of class a"
class_b["name"]: "name of class b"
class_b["description"] : "description of class b"

Upvotes: 1

Arjun Singh
Arjun Singh

Reputation: 312

You need send data in the following format:

{
    class_a: {
        name: "name of class a",
        description: "description of class a"
    },
    class_b: {
        name: "name of class b",
        description: "description of class b"
    }
}

Upvotes: 1

Rudson Rodrigues
Rudson Rodrigues

Reputation: 345

To use the shape as if it was an Array, you can create a previous shape and update with the values you are receiving. Try to take a look at how to update Array - https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/observe

Upvotes: 1

Related Questions