Skip to content
Advertisement

How can I split string elements of an array and add them to another?

Using an array list of names, I am trying to split the first names and surnames using a for loop. The code works but when splitting the names using a substring it goes from 0-x (x being the space between the names) but it reads from 0 each time and adds each name multiple times until it completes. How can I run it from the next element each time to skip the name previously split and added?

JavaScript

For example the output of first names is like this: lee lee, michael lee, michael, kim lee, michael, kim, dean

Advertisement

Answer

Fix

You need only one loop, to extract both

JavaScript

Improve

  • use String.split()
  • use a for each loop
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement