I have a directory called Polygons
where I have created an index.js
file to export all the files from the directory. It looks something like this:
export {default as europe} from './europe'
export {default as northAmerica} from './northAmerica'
europe
file exports variable like this:
export default europe;
And in the file where I would like to use this variables I am importing them like this:
import * as regions from './Polygons'
I would like to iterate somehow now over those imports, is there any way of doing this?
I have tried doing:
for (const element of regions) {
console.log(element);
}
But, then I get:
Uncaught (in promise) TypeError: Invalid attempt to iterate non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
I have a directory called Polygons
where I have created an index.js
file to export all the files from the directory. It looks something like this:
export {default as europe} from './europe'
export {default as northAmerica} from './northAmerica'
europe
file exports variable like this:
export default europe;
And in the file where I would like to use this variables I am importing them like this:
import * as regions from './Polygons'
I would like to iterate somehow now over those imports, is there any way of doing this?
I have tried doing:
for (const element of regions) {
console.log(element);
}
But, then I get:
Uncaught (in promise) TypeError: Invalid attempt to iterate non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
Share
Improve this question
edited Apr 26, 2020 at 14:37
Leff
asked Apr 26, 2020 at 14:13
LeffLeff
1,35031 gold badges111 silver badges224 bronze badges
1 Answer
Reset to default 14It is an object, so for..of will fail but you can use Object.keys
or Object.values
to convert it into array and then get the results.
Here is a sample code which is doing the same:
keys = Object.keys(React)
for (const key of keys) {
console.log(key, React[key])
}
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Hope it helps. Revert for any doubts/clarifications.