I am consuming an API of countries. I enter the country in an input and all the data is returned to me. Among these data is the following:
data = {
"currencies": {
"CLP": {
name: "pais",
valor: "50"
}
}
}
I'm destructuring the values I need, but when I destruct this one, I can't think of how. The "CLP" varies from the country you enter, if you enter Peru, instead of CLp you will get PEN. I need to extract name
and value. So far I have this:
const { currencies} = data;
const {CLP:otro} = data.currencies;
const {} = data.currencies;
But of course, neither works. How can I indicate that this key/key will be dynamic and be able to extract the data?
If you are sure that it is always the only data that brings you in currencies, you can do it like this:
If you want to apply the destructuring with dynamic values according to the documentation , you can do it in the following way, using the classic
clave:valor
.