I have 3 different arrays in JavaScript, they have always the same length or may be not and I need to change response of this array in JSON format
I am getting this below response right now.:-
"status": "ok",
"massage": "success"
"data":{
"Investment":[
"file":[
"654g6-8557-6545.xls",
"45yj-5667-5567.docx",
"305fh5-5547-5566.png"
],
"nameOfFile":[
"Aml_MasterFile.xsx",
"Value_policy.docx",
"Image1.png"
],
"file type":[
"Purchase agreement",
"option agreement",
"Profile"
]
]
}
**Expected Response** is:
"status": "ok",
"massage": "success"
"data":[
{
"file":"654g6-8557-6545.xls",
"nameOfFile": "Aml_MasterFile.xsx",
"file_type":"purchase agreement"
},
{
"file":"45yj-5667-5567.docx",
"nameOfFile":"Value_policy.docx",
"file_type":"option agreement"
},
{
"file":"305fh5-5547-5566.png",
"nameOfFile":"Image1.png",
"file_type":"profile"
}
]
**My Code:** can achieve this with just 1 map??????
Var investor_id = req param("investor_id")
Var condition = {"is_deleted":{"$ne":"1"}}
var result = await db. collection("dysinvestor").aggregate([
{"$match" : condition},
{"$project": {
"_id":0,
"filename":"$formData.file1",
"original name":"$formData.origin_file1",
"filetype":"$formData.file1_filetype",
}},
]).toArray()
**My Databases Records for example**-
{ "_id" : "154646116164654", "formData" : { "file1" : [ "dsd-3ccc-47da-ds-18659sdssd98d612c.pdf", "31610233-a813-4b06-8d34-4277f055d4d2.pdf" ], "origin_file1" : [ "Sample_00.pdf", "Sample_00.pdf" ], "file1_filetype" : [ "006", "002" ],
I would appreciate any help... Thanks in advance ☺️
Dont know if I got your problem... I've used your input as sample data, the convert function transfers the input data to your desired structure...
let input = {
"status": "ok",
"massage": "success",
"data":{
"Investment":{
"file":[
"654g6-8557-6545.xls",
"45yj-5667-5567.docx",
"305fh5-5547-5566.png"
],
"nameOfFile":[
"Aml_MasterFile.xsx",
"Value_policy.docx",
"Image1.png"
],
"file type":[
"Purchase agreement",
"option agreement",
"Profile"
]
}
}
}
function convert(arg) {
let result = {
"status": arg.status,
"message": arg.massage,
"data": []
}
// TODO: instead of hardcoded array length, determine the largest array and use that as loop condition
for (let i = 0; i < arg.data.Investment.file.length; i++) {
// TODO: implement check to see if the array index exists
let obj = {
"file": arg.data.Investment.file[i],
"nameOfFile": arg.data.Investment.nameOfFile[i],
"file_type": arg.data.Investment["file type"][i]
}
result.data.push(obj);
}
return result;
}
let tmp = convert(input);
console.log(tmp);