oslon Posted November 1, 2023 Share Posted November 1, 2023 TypeError: arrayOfObjects is not iterable at Object.<anonymous> (C:\Users\User\Desktop\100DAYOFCODE\1\1\script.js:3:19) at Module._compile (node:internal/modules/cjs/loader:1256:14) at Module._extensions..js (node:internal/modules/cjs/loader:1310:10) at Module.load (node:internal/modules/cjs/loader:1119:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47 The code in question: const arrayOfObjects = require("./data"); for (const obj of arrayOfObjects) { console.log(`ID: ${obj.id}, Name: ${obj.name}`); } data is a file containing stuffs in this order const arrayOfObjects = [ { "id": 1, "name": "bla bla" }, { "id": 2, "name": "bla bla bla" } ]; Any ideas to resolve this issue? Quote Link to comment https://forums.phpfreaks.com/topic/317405-typeerror-arrayofobjects-is-not-iterable/ Share on other sites More sharing options...
Solution Strider64 Posted November 1, 2023 Solution Share Posted November 1, 2023 // Import the arrayOfObjects from the data module. const arrayOfObjects = require("./data"); // Iterate through each object in the arrayOfObjects. for (const obj of arrayOfObjects) { // Log the id and name properties of the current object to the console. console.log(`ID: ${obj.id}, Name: ${obj.name}`); } For the data module that contains the array of objects: // Define an array of objects with properties id and name. const arrayOfObjects = [ { "id": 1, "name": "bla bla" }, // Object with id 1 and name 'bla bla' { "id": 2, "name": "bla bla bla" } // Object with id 2 and name 'bla bla bla' ]; // Export the arrayOfObjects so it can be imported and used in other modules. module.exports = arrayOfObjects; Quote Link to comment https://forums.phpfreaks.com/topic/317405-typeerror-arrayofobjects-is-not-iterable/#findComment-1612643 Share on other sites More sharing options...
requinix Posted November 1, 2023 Share Posted November 1, 2023 Because require() isn't like PHP where you "require" a file and then you get the variables in it. In Javascript, each file is like an independent module, so if you want to share - or export - data from a file then you need to use module.exports as Strider64 showed. What require() will then do, internally, is run the file and then return to you what was exported. If you wanted to export just the one value then you can set module.exports like that. If you had multiple values then you can treat module.exports as an object (which is what it is by default) and set properties on it; this pairs well with destructuring. const arrayOfObjects = [ ... ]; const anotherArrayOfObjects = [ ... ]; module.exports = { arrayOfObjects, anotherArrayOfObjects }; const { arrayOfObjects } = require("./data"); Quote Link to comment https://forums.phpfreaks.com/topic/317405-typeerror-arrayofobjects-is-not-iterable/#findComment-1612648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.