Im seeking a way to write a function which destructures fetched data. The structure of the data differs from time to time but the structure is knowen by deriving from an graphql query.
Even more context: For an react project im fetching data from an api. The specific data is tailored for each component but the fetch logic is shared within a custom hook. I want to source the destructuring assignments in external logic aswell. If there is a better or common aproach to this it is very welcome maby linked as a comment?
Knowns:
- datastructure for destructuring assignment
- data
Question:
Is there a way to store a destructuring assignment in a variable and destructure with it?
Example:
//const data = fetch(query)
//data could also be: {"a":"b", "c":"d", "e":"f"} || {"a":{"b": "c"}} || ect...
I try achieve somthing like this:
const assignment = `{ a, c, d, e }` //would be returned by an function
const destructuring = ((assignment) =>{
logic(a, c, d, e)
})
destructuring(data)
or like this:
const assignment = `{ a, c, d, e }`
const assignment = data