Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object is not iterable - NodeJS

My following NodeJS program gives me the error when I try to print them it stops at dataGood, because it is not iterable.

JSON

{"name":"something,cool", "user": "Awesome,Great"}

Script

if(fs.existsSync('./data/data.json')){
  let data =  fs.readFileSync('./data/data.json');
  let dataGood = JSON.parse(data);
  let nameGood = [];
  let userGood = [];
  for(const element of dataGood){
      let name = [] = element.name;
      let user = [] = element.user;
      let nameSplit = name.toString().split(',')
      let userSplit = user.toString().split(',');
      nameGood = nameSplit;
      userGood = userSplit;
  }
console.log(nameGood, userGood)
}

Error

TypeError: dataGood is not iterable

Objective

I want to bring the "name" and "user" from JSON into those two arrays, and split them by comma.

//After script runs
nameGood  = ['something','cool']
userGood  = ['Awesome','Great']
like image 790
Mazino Avatar asked Feb 20 '26 22:02

Mazino


1 Answers

for(const [key, value] of Object.entries(dataGood)){
      ...
  }

or

for(const element of Object.keys(dataGood)){
      ...
  }
like image 130
TuritCha Avatar answered Feb 23 '26 12:02

TuritCha