I have reorganized the following code to enhance simplicity and reusability. Essentially, my goal is straightforward: I possess a series of empty arrays that are linked to specific keys (which correspond to a set of unique ID's).
To populate data into these various arrays, I utilize a common API call. In this API call, the request payload includes the ID of the reference data. Upon receiving the response, I aim to identify the variable name inside the object associated with the ID and store the received value there before moving on to the next iteration of values/IDs.
For instance, if the ID value is 1, then the request payload will also include an ID of one. The corresponding response data should be stored in the value matched[1], like 'fruits' array mentioned earlier (not the object value itself, but the array named "fruits"). How can I achieve this?
let fruits=[]
let vegetables=[]
let nuts=[] ......etc up to n number of entries
let mapped={1:fruits,2:vegetables,3:nuts,.......(and other 'n' entries) }
for(let i=1;i<=15;i++){
let payload={id:i}
this.apiService.fetchReferenceData(payload).then((response)=>{
mapped[i]=response.data //the received value must be stored in the array corresponding to the name of the object value
})
}
}