Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

loop data into object javascript and returning the value

i try loop data into empty object but it turns out the data after i console.log the data only return one object wher, can someone tell me where i did wrong here

const data_warehouse = forms.map((item) => {
        item.answer.map((data) => {
          let data_fix = {};
          Object.keys(data.answers).map((key) => {
            return (data_fix = {
              [key.replace(/ /g, "_").toLowerCase()]: data.answers[key],
            });
          });
          console.log(data_fix);
        });
      });

here is the data from mongoDB:

"forms": [
        {
            "_id": "Quality",
            "title": "Quality",
            "answer": [
                {
                    "username": "Jansenstan24@gmail.coms",
                    "date": "2022-10-25",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "Text": "john@dose.com",
                        "Email": "john@nabatisnack.com",
                        "Plant": "Cica"
                    }
                },
                {
                    "username": "adam@wegodev.com",
                    "date": "2022-10-25",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "Email": "john@dose.coms",
                        "Plant": "Ranca",
                        "Text Doang": "12"
                    }
                },
                {
                    "username": "jansenstan24@gmail.com",
                    "date": "2022-10-31",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "Text": "john@nabatisnack.com",
                        "Email": "john@dose.com",
                        "Plant": "Cica"
                    }
                }
            ]
        }
    ]

i try to redesign the key on field "answers" to change all the specials characters into underscore, but its only returning one data after i loop it into map function:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{ plant: 'Cica' }
{ text_doang: '12' }
{ plant: 'Cica' }

my expected result should be like this,it will return the same format but only cleanup the special characters from the key on field "answers":

 [
        {
            "_id": "Quality",
            "title": "Quality",
            "answer": [
                {
                    "username": "Jansenstan24@gmail.coms",
                    "date": "2022-10-25",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "text": "john@dose.com",
                        "email": "john@nabatisnack.com",
                        "plant": "Cica"
                    }
                },
                {
                    "username": "adam@wegodev.com",
                    "date": "2022-10-25",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "email": "john@dose.coms",
                        "plant": "Ranca",
                        "text_doang": "12"
                    }
                },...etc]

>Solution :

try this

var forms=[
        {
            "_id": "Quality",
            "title": "Quality",
            "answer": [
                {
                    "username": "Jansenstan24@gmail.coms",
                    "date": "2022-10-25",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "Text": "john@dose.com",
                        "Email": "john@nabatisnack.com",
                        "Plant": "Cica"
                    }
                },
                {
                    "username": "adam@wegodev.com",
                    "date": "2022-10-25",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "Email": "john@dose.coms",
                        "Plant": "Ranca",
                        "Text Doang": "12"
                    }
                },
                {
                    "username": "jansenstan24@gmail.com",
                    "date": "2022-10-31",
                    "formId": "6357921d49de88bb7fffcfe4",
                    "answers": {
                        "Text": "john@nabatisnack.com",
                        "Email": "john@dose.com",
                        "Plant": "Cica"
                    }
                }
            ]
        }
    ]
  var newData=  forms.map((item) => {
        var mappedAns=item.answer.map((data) => {
          let data_fix = {};
          Object.keys(data.answers).forEach((key) => {
            data_fix[key.replace(/ /g, "_").toLowerCase()]= data.answers[key];
          });
          return {...data,answers:data_fix};
        });
        return {...item,answer:mappedAns}
      });
      
 console.log(newData);
      
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading