Although this question may seem similar to others, my case is unique. I have already implemented a solution using a Synchronous loop, but now I want to switch to an Asynchronous loop. Unfortunately, I am struggling to make this transition.
Synchronous loop Solution :
Data input is retrieved from an API.
let input = [
{
"id":1,
"domain":"bank",
"projects":[
{
"id":1,
"name":"home loan",
"amount":["123","345","45"]
},
{
"id":2,
"name":"car load",
"amount":["123","34","45"]
}
]
},
{
"id": 2,
"domain":"game",
"projects":[
{
"id":3,
"name":"cricket",
"amount":["13","4","45"]
},
{
"id":4,
"name":"Kabadi",
"amount":["3","4","5"]
}
]
}
];
const transformation = data => {
let counter = 0;
let domainProjects = [];
for (let index = 0; index < data.length; index++) {
let len = data[index].projects;
for (let j = 0; j < len.length; j++) {
domainProjects[counter] = {
domain: data[index].domain,
project: len[j].name,
amount: len[j].amount,
};
counter++;
}
}
return domainProjects;
};
const result = transformation(input);
console.log("Result:",result);
Asynchronous loop expected result :
expectResult =[
{
"domain":"bank",
"name":"home loan",
"amount":["123","345","45"]
},
{
"domain":"bank",
"name":"car load",
"amount":["123","34","45"]
},
{
"domain":"game",
"name":"cricket",
"amount":["13","4","45"]
},
{
"domain":"game",
"name":"Kabadi",
"amount":["3","4","5"]
}
];