0

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 ?

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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"
    }
}

Comments

1

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"

Comments

1

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"});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.