I want to .map instead of for loop but I still confuse how to use it here is my data
const arr = [
{
name: "John",
subject : [
{
subject_id : 1,
score :20
}
]
},
{
name: "Doe",
subject : [
{
subject_id : 2,
score :20
}
]
}
]
I want to add subject detail into subject array
async getData() {
for (let i in arr) {
let data = arr[i];
for (let i in data.subject) {
const subject = data.subject[i];
//this line code will get subject detail
const getSubject = await this.getSubject(subject);
subject["subjectData"] = getSubject;
}
}
}
everything work fine but How can I use map instead of for loop
example for output
{
name: "Doe",
subject : [
{
subject_id : 2,
score :20,
subjectData: {
//some detail
}
}
]
}
Here is what I want to do
const newData = arr.map(async data => {
const getSubject = data.subject;
const subject = getSubject.map(async zp02 => {
const getSubject = await this.getSubject(subject);
return { getSubject };
});
return { ...data, subject };
});
my subject always return null .
the reason that I want to use map because I've read many people say It faster than for loop
- Is their anyway to use map instead of for
- I'm learning about clean code can you give me an Idea how to refactor code in nested for loop