My following NodeJS program gives me the error when I try to print them it stops at dataGood, because it is not iterable.
{"name":"something,cool", "user": "Awesome,Great"}
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)
}
TypeError: dataGood is not iterable
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']
for(const [key, value] of Object.entries(dataGood)){
...
}
or
for(const element of Object.keys(dataGood)){
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With