Home > front end >  loop through returned results node.js
loop through returned results node.js

Time:04-09

I am getting results returned from my request made to my server. It is returning multiple lines, in which each line is a new entry into the array.

So right now I am logging it as:

completion.data.choices[0].text

and it is obviously only returning the first one. But i want it to return all of them. How would I do this?

CodePudding user response:

If you're just trying to log all the choices, you can use a forEach loop

completion.data.choices.forEach(choice => {
  console.log(choice.text);
});

CodePudding user response:

Try this

 completion.data.choices.forEach(item => {
        console.log(item.text)
    }
  • Related