I have an object that I iterate through. It has two levels so I do two Object.entries iterations.
The end result I need is a 2D array like : [[1,2,3],[1,2,3]] but somehow I always get [1,2,3,1,2,3]. I need the first type of array due to a setValues() operation later on to write the data on an Excel.
Here is the code with one iteration through the object, and inside a second iteration where I say : if answer doesn't exist, push a blank space because the array needs a certain length (due to headers).
for (const [key, value] of Object.entries(result)) {
answerID.push([value["id"],value["created_at"],value["form_id"],value["updated_at"]])
for (const [subKey,subValue] of Object.entries(value["answers"])){
if (subValue.hasOwnProperty("answer")) {
answerDetail.push(subValue["answer"]);
}
else{
answerDetail.push("");
}
}
answerArray.push(answerDetail)
// I also tried answerArray.push([answerDetail]) but then it returns [[[1,2,3,1,2,3]]]
}
EDIT : by request, I write the structure of result although being full of private data, I can only write a edited version of it. I used JSON formatter to check if my edit is still coherent.
[{"id":"50993654","form_id":"212597363","ip":"","created_at":"2021-10-13 08:15:47","status":"ACTIVE","new":"1","flag":"0","notes":"","updated_at":null,"answers":{"1":{"name":"partie1","order":"2","text":"Partie 1 - QCM - Vous avez une 45 minutes pour soumettre le questionnaire.","type":"control_head"},"3":{"cfname":"Compte à rebours global","name":"saisissezUne","order":"5","selectedField":"","static":"No","text":"","type":"control_widget"},"4":{"name":"email4","order":"3","text":"E-mail","type":"control_email","answer":""},"7":{"name":"prenomprenom","order":"4","text":"Prénom/Prénom","type":"control_textbox","answer":""}}},{"id":"5099284185","form_id":"212592657363","ip":"","created_at":"2021-10-13 07:17:54","status":"ACTIVE","new":"1","flag":"0","notes":"","updated_at":null,"answers":{"1":{"name":"partie1","order":"2","text":"Partie 1 - QCM - Vous avez une 45 minutes pour soumettre le questionnaire.","type":"control_head"},"3":{"cfname":"Compte à rebours global","name":"saisissezUne","order":"5","selectedField":"","static":"No","text":"","type":"control_widget"},"4":{"name":"email4","order":"3","text":"E-mail","type":"control_email","answer":""},"7":{"name":"prenomprenom","order":"4","text":"Prénom/Prénom","type":"control_textbox","answer":""}}}]
Thank if you can help me with this,
Cedric
CodePudding user response:
You can use the .map
function to change each row by targeting only the answers
key, then use Object.values
to retrieve an array of answers
's contents and remap
that with the name
key.
let str = [{
"id": "50993654",
"form_id": "212597363",
"ip": "",
"created_at": "2021-10-13 08:15:47",
"status": "ACTIVE",
"new": "1",
"flag": "0",
"notes": "",
"updated_at": null,
"answers": {
"1": {
"name": "partie1",
"order": "2",
"text": "Partie 1 - QCM - Vous avez une 45 minutes pour soumettre le questionnaire.",
"type": "control_head"
},
"3": {
"cfname": "Compte à rebours global",
"name": "saisissezUne",
"order": "5",
"selectedField": "",
"static": "No",
"text": "",
"type": "control_widget"
},
"4": {
"name": "email4",
"order": "3",
"text": "E-mail",
"type": "control_email",
"answer": ""
},
"7": {
"name": "prenomprenom",
"order": "4",
"text": "Prénom/Prénom",
"type": "control_textbox",
"answer": ""
}
}
},
{
"id": "5099284185",
"form_id": "212592657363",
"ip": "",
"created_at": "2021-10-13 07:17:54",
"status": "ACTIVE",
"new": "1",
"flag": "0",
"notes": "",
"updated_at": null,
"answers": {
"1": {
"name": "partie1",
"order": "2",
"text": "Partie 1 - QCM - Vous avez une 45 minutes pour soumettre le questionnaire.",
"type": "control_head"
},
"3": {
"cfname": "Compte à rebours global",
"name": "saisissezUne",
"order": "5",
"selectedField": "",
"static": "No",
"text": "",
"type": "control_widget"
},
"4": {
"name": "email4",
"order": "3",
"text": "E-mail",
"type": "control_email",
"answer": ""
},
"7": {
"name": "prenomprenom",
"order": "4",
"text": "Prénom/Prénom",
"type": "control_textbox",
"answer": ""
}
}
}
]
let r = str.map(i => Object.values(i.answers).map(ii => ii.name))
console.log(r)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>