Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

create an array form a string

So I am getting back a string where each 'section' is being delimited by a semicolon.

"Active; Texas; Smith, John; Adam;"

I have an on click or on selected function happening that will pass that string along but I need to take it and split it so Active goes to one div, Texas to another div Smith to to a div and John to a div as well as Adam to a div (I realize Smith and John are coma delimited).

While I realize I can do

    // My string comes from JQuery UI's Autocomplete as ui.item.label
     var array = ui.item.value.split(';');
     console.log( array );

problem is some times there wont be a middle name or a location so my string might look like "Inactive; ; Smith, John; ;" I can't have my order messed up.

1 Answer

Steven Parker
Steven Parker
243,318 Points

It's not clear what the problem is.

You said: ...my string might look like "Inactive; ; Smith, John; ;"

This should still separate into the same array elements, where the missing parts will only have a space. So your order should be preserved.

So what is the problem?

your right, due to caching problems between me and the dev server some code didnt update so with a prior attempt I was getting back less items in my array. with the above solution, and the cache cleared out running the new code it worked.