0

I'm trying to loop over an array of objects to call an api. I destructure the return values but because I am making multiple requests I need to give them unique alias names. Is there a way to use a dynamic name for the alias like:

{data: section.id + "Data"}

const sections = [
    {id: 'one', api: '/api/one'},
    {id: 'two', api: '/api/two'},
];



sections.forEach((section) => {
    const { data: section.id + "Data", error: section.id + "Error" } = useFetch(() => 
        section.url, {
        query: section.query,
    });
});
Neatpixels
  • 75
  • 1
  • 9
  • But why? Why would that be useful? – kelsny Mar 15 '23 at 19:11
  • "*because I am making multiple requests I need to give them unique alias names.*" no you don't. Each variable can have the same name, since each is created in a different scope. – VLAZ Mar 15 '23 at 19:11

0 Answers0