Skip to content
Advertisement

Why split after new line is not returning single array using JavaScript?

I have a string text after fetching an API with a for loop, that returns me a list of names, each name separated by a new line. When I use the code in the snippet, it returns me each name in an individual array, instead of a single array. Is there anything wrong with the RegEx? Could someone please tell me what is wrong? Thank you very much for the help.

JavaScript
JavaScript

Advertisement

Answer

Your JSON data is an array of items. Not all items as one string seperated by a newline character.

The easiest way to get the names of the items, is to use a map and return the name of the item.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement