Home > Net >  is there a possible way to put the object Json outsite the object
is there a possible way to put the object Json outsite the object

Time:11-03

is there a way to change this json

"forms": [
    {
        "_id": "Untitled Form",
        "title": "Untitled Form",
        "answer": [
            {
                "username": "[email protected]",
                "date": "2022-11-02",
                "formId": "6361c5aaf7a02c177ebebb27",
                "answers": {
                    "test": [
                        "New Option"
                    ],
                    "email": "[email protected]",
                    "dropdown": "New Option",
                    "radio": "3",
                    "date": "2022-11-01T00:00:00.000Z",
                    "time": "17:25"
                }
            },
            {
                "username": "[email protected]",
                "date": "2022-11-03",
                "formId": "6361c5aaf7a02c177ebebb27",
                "answers": {
                    "test": [
                        "New Option"
                    ],
                    "email": "[email protected]",
                    "dropdown": "3",
                    "radio": "3",
                    "date": "2022-11-16T00:00:00.000Z",
                    "time": "09:44"
                }
            }
        ]
    }
]

i try to loop it using javascript but it dont return the same value as i expected and only increasing the value into the array.., i try some logic but it wont return the same value as i want also

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

CodePudding user response:

A simple way is using the spread operator (...) to merge data and data.answer objects. Then remove the answer property from the object via delete.

let forms = [{
    "answer": [
      {
        "answer": {
          "6357921d49de88bb7fffcfe8": "[email protected]",
          "6357b331235053a9d4e8d037": "[email protected]",
          "6357b335235053a9d4e8d03a": "Maja",
          "Email": "[email protected]",
          "Plant": "Maja",
          "Text": "[email protected]"
        },
        "date": "2022-10-25",
        //"formId": ObjectId("6357921d49de88bb7fffcfe4"),
        "formId": "6357921d49de88bb7fffcfe4",
        "username": "[email protected]"
      },
      {
        "answer": {
          "6357921d49de88bb7fffcfe8": "[email protected]",
          "6357b331235053a9d4e8d037": "[email protected]",
          "6357b335235053a9d4e8d03a": "Cica",
          "Email": "[email protected]",
          "Plant": "Cica",
          "Text": "[email protected]"
        },
        "date": "2022-10-25",
        //"formId": ObjectId("6357921d49de88bb7fffcfe4"),
        "formId": "6357921d49de88bb7fffcfe4",
        "username": "[email protected]"
      }
    ]
}];

const newData = forms.map((item) => {
    var mappedAns = item.answer.map((data) => {
        var obj = { ...data.answer, ...data };
        delete obj.answer;

        return obj;
    })

    return { ...item, answer: mappedAns };
});

console.log(newData);

CodePudding user response:

 const newData = forms.map((item) => {
        var mappedAns = item.answer.map((data) => {
          Object.keys(data.answers).forEach((key) => {
            data[
              key
                .replace(/[{()}]/g, "")
                .replace(/ :/g, "")
                .replace(/ /g, "_")
                .toLowerCase()
            ] = data.answers[key];
          });
          delete data.answers;
          return { ...data };
        });
        return { ...item, answer: mappedAns };
      });
  • Related